0

What is the way of combining predicates like we used do for Expression.AndAlso

I have a predicate

Predicate<Foo> predicate = p1 => (p1.IsActive) && p1.Type != "BR";

and

Predicate<Foo> datePredicate = p1 => (p1.Year) == DateTime.Now.Year;

Based on some condition I want to combine both so that resultant predicate can be passed to a method which accepts single predicate parameter.

predicate = predicate.????(datePredicate);

so what is the way to do it?

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208

1 Answers1

1

You can combine predicates as below

Predicate<Foo> datePredicateAnd = p =>(predicate(p) && datePredicate(p));
Damith
  • 62,401
  • 13
  • 102
  • 153
  • 1
    Even though this will also work `predicate = p => predicate(p) && datePredicate(p);` but this will cause StackOverflow Exception as it will try to call itself resulting in StackOverflow. A perfect example of `Access to modified Closure.` – Nikhil Agrawal Nov 14 '16 at 09:19