Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to 'not' a lambda expression for entity framework

Given the following

Expression<Func<T,bool>> matchExpression;

How can i create another expression that is a 'not' of the existing one.

i have tried

Expression<Func<T, bool>> func3 = (i) => !matchExpression.Invoke(i);

but this is not supported by the entity framework...

Regards

like image 615
Richard Friend Avatar asked Sep 24 '10 11:09

Richard Friend


1 Answers

You have to recreate a new lambda, and negate the body of the original one:

Expression<Func<T, bool>> not = Expression.Lambda<Func<T, bool>> (
    Expression.Not (matchExpression.Body),
    matchExpression.Parameters [0]);
like image 51
Jb Evain Avatar answered Oct 10 '22 00:10

Jb Evain