Skip to content

Instantly share code, notes, and snippets.

@akunzai
Created November 17, 2017 18:01
Show Gist options
  • Select an option

  • Save akunzai/49506f9cd2e07136c75ec581159ce0dd to your computer and use it in GitHub Desktop.

Select an option

Save akunzai/49506f9cd2e07136c75ec581159ce0dd to your computer and use it in GitHub Desktop.
Combine two Linq predicate expression
namespace System.Linq.Expressions
{
public static class ExpressionExtensions
{
public static Expression<Func<T,bool>> And<T>(this Expression<Func<T,bool>> left, Expression<Func<T, bool>> right)
{
if (left == null) return right;
var and = Expression.AndAlso(left.Body, right.Body);
return Expression.Lambda<Func<T, bool>>(and, left.Parameters.Single());
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
if (left == null) return right;
var and = Expression.OrElse(left.Body, right.Body);
return Expression.Lambda<Func<T, bool>>(and, left.Parameters.Single());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment