我有我該如何結合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