我有两个类型为Expression<Func<string, bool>>
的表达式树,我想获得一个对两个表达式执行OR运算的表达式(将相同的字符串参数传递给两个表达式)知道吗?
您可以使用LINQKit中的PredicateBuilder
来执行此操作。例如:
Expression<Func<string, bool>> e1 = …;
Expression<Func<string, bool>> e2 = …;
Expression<Func<string, bool>> combined = e1.Or(e2).Expand();
您可以尝试将它们组合到Expression.Lambda
表达式中,然后使用Expression.Or
检查其中一个是否为true。
这里有一个例子:
Expression<Func<Car, bool>> theCarIsRed = c1 => c1.Color == "Red";
Expression<Func<Car, bool>> theCarIsCheap = c2 => c2.Price < 10.0;
Expression<Func<Car, bool>> theCarIsRedOrCheap = Expression.Lambda<Func<Car, bool>>(
Expression.Or(theCarIsRed.Body, theCarIsCheap.Body), theCarIsRed.Parameters.Single());
var query = carQuery.Where(theCarIsRedOrCheap);
也许你可以在这里获得更多信息