簡體   English   中英

如何結合表情 <Func<MyClass,bool> &gt; []?

[英]how do I combine 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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM