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).