I'm seeking for a way to negate an expression used to filter IQueryable
sequences.
So, I've got something like:
Expression<Func<T, bool>> expression = (x => true);
Now I wish to create the expression which would result in yielding (x => false)
- so I basically want to negate the expression
.
The working method I've found myself works like this:
var negatedExpression =
Expression.Lambda<Func<T, bool>> (Expression.Not(expression.Body),
expression.Parameters[0])));
But I'm almost sure there is a better way - could you help me? (something like Not(expression)
, probably).
An easy extension method:
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> one)
{
var candidateExpr = one.Parameters[0];
var body = Expression.Not(one.Body);
return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
}
Usage:
Expression<Func<int, bool>> condition = x => x > 5;
var source = Enumerable.Range(1, 10);
var result1 = source.Where(condition.Compile()); //6,7,8,9,10
var result2 = source.Where(condition.Not().Compile()); //1,2,3,4,5