Created
December 21, 2012 04:24
-
-
Save tncbbthositg/4350639 to your computer and use it in GitHub Desktop.
Extension method for combining predicates and extension method for or conditions in linq queries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IQueryable<T> WhereAny<T>(this IQueryable<T> source, params Expression<Func<T, bool>>[] predicates) | |
{ | |
Expression<Func<T, bool>> filter = t => false; | |
foreach (var predicate in predicates) | |
filter = filter.Combine(Expression.OrElse, predicate); | |
return source.Where(filter); | |
} | |
private static Expression<Func<T, bool>> Combine<T>(this Expression<Func<T, bool>> predicate, Func<Expression, Expression, BinaryExpression> combination, Expression<Func<T, bool>> withPredicate) | |
{ | |
var invocation = Expression.Invoke(withPredicate, predicate.Parameters); | |
var combined = combination(predicate.Body, invocation); | |
return Expression.Lambda<Func<T, bool>>(combined, predicate.Parameters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment