我有我该如何结合Expression <Func <MyClass,bool >> []?
Expression<Func<MyClass,bool>>
但是一个数组,我想和他们一起获得该类型的只是一个单一的项目。我该怎么做呢?我可以投出Expression.And的结果吗?
我有我该如何结合Expression <Func <MyClass,bool >> []?
Expression<Func<MyClass,bool>>
但是一个数组,我想和他们一起获得该类型的只是一个单一的项目。我该怎么做呢?我可以投出Expression.And的结果吗?
如果您使用下面的扩展方法:
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);
}
从这里:http://www.albahari.com/nutshell/predicatebuilder.aspx
然后,你可以写这个折叠他们都到一个表达。
public Expression<Func<T, bool>> AggregateAnd(Expression<Func<T,bool>>[] input)
{
return input.Aggregate((l,r) => l.And(r));
}
结果是,上面的代码实际上并不适用于大多数目的,因为Invoke无法转换为SQL。我结束了在这里找到类似但更兼容的代码:http://stackoverflow.com/questions/110314/linq-to-entities-building-where-clauses-to-test-collections-within-a-many-to-m – Brannon
你会以样品显示吗? –
我在过去使用PredicateBuilder来做到这一点 - http://www.albahari.com/nutshell/predicatebuilder.aspx –
你想最终的结果也是'Expression>'? –
yamen