Skip to content

Instantly share code, notes, and snippets.

@tncbbthositg
Created December 21, 2012 04:24
Show Gist options
  • Save tncbbthositg/4350639 to your computer and use it in GitHub Desktop.
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.
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