开发者

how to 'not' a lambda expression for entity framework

开发者 https://www.devze.com 2023-01-17 16:04 出处:网络
Given the following Expression<Func<T,bool>> matchExpression; How can i create another expression that is a \'not\' of the existing one.

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 fra开发者_开发百科mework...

Regards


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]);
0

精彩评论

暂无评论...
验证码 换一张
取 消