Nikhil Agrawal Nikhil Agrawal - 1 year ago 98
C# Question

Combining Predicates in post .Net 2.0

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


I have a predicate

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


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?

Answer Source

You can combine predicates as below

Predicate<Foo> datePredicateAnd = p =>(predicate(p) && datePredicate(p));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download