[英]how do I combine Expression<Func<MyClass,bool>>[]?
I have an array of 我有一系列
Expression<Func<MyClass,bool>>
However, I want to AND them all together to get just a single item of that type. 但是,我想将它们全部“与”在一起,以得到该类型的单个项目。 How do I do this? 我该怎么做呢? Can I cast the result of Expression.And? 我可以转换Expression.And的结果吗?
If you use the following extension method: 如果使用以下扩展方法:
public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
}
From here: http://www.albahari.com/nutshell/predicatebuilder.aspx 从这里: http : //www.albahari.com/nutshell/predicatebuilder.aspx
Then you can just write this to fold them all down to one expression. 然后,您只需编写此代码即可将它们全部折叠为一个表达式。
public Expression<Func<T, bool>> AggregateAnd(Expression<Func<T,bool>>[] input)
{
return input.Aggregate((l,r) => l.And(r));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.