Nikhil Agrawal Nikhil Agrawal - 25 days ago 9
C# Question

Combining Predicates in post .Net 2.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?

Answer

You can combine predicates as below

Predicate<Foo> datePredicateAnd = p =>(predicate(p) && datePredicate(p));