如何'不'实体框架的lambda表达式

时间:2021-02-04 19:05:34

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

问候

1 个解决方案

#1


10  

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

你必须重新创建一个新的lambda,并否定原始lambda的主体:

Expression<Func<T, bool>> not = Expression.Lambda<Func<T, bool>> (
    Expression.Not (matchExpression.Body),
    matchExpression.Parameters [0]);

#1


10  

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

你必须重新创建一个新的lambda,并否定原始lambda的主体:

Expression<Func<T, bool>> not = Expression.Lambda<Func<T, bool>> (
    Expression.Not (matchExpression.Body),
    matchExpression.Parameters [0]);

相关文章