Created
November 17, 2017 18:01
-
-
Save akunzai/49506f9cd2e07136c75ec581159ce0dd to your computer and use it in GitHub Desktop.
Combine two Linq predicate expression
This file contains hidden or 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
| 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