1

I have a list of lambda-expressions List<Func<SomeObject, bool>> filterList; This filterlist is using to filter a collection of SomeObject easily like this:

List<SomeObject> randomList; //filled with random stuff

foreach (Func<SomeObject, bool> filter in filterlist)
    randomList = randomList.Where(filter).ToList();

Now I want to combine some filters - but I want to combine them with AND- or OR-Statements. As example: The user has 3 filter A,B and C and want to combine them to something like "A && (B || C).

I have no idea howto do this.

akop
  • 5,981
  • 6
  • 24
  • 51

1 Answers1

2

You are using Func<T>, not Expression<Func<T>>, which makes it impossible to 'rewrite' them, or combine multiple func's in a single one. So you can only execute them in a certain order.

You are already combining them with AND.

To combine two (or more) criteria with OR, do this:

randomList = randomList.Where(x => filter1(x) || filter2(x)).ToList();

To do A && (B || C), do this:

randomList = randomList.Where(x => filterA(x) && (filterB(x) || filterC(x))).ToList();
Maarten
  • 22,527
  • 3
  • 47
  • 68