简体   繁体   English

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

[英]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.

相关问题 如何从Expression <Func <MyClass,string >>动态创建Expression <Func <MyClass,bool >>谓词? - How do I dynamically create an Expression<Func<MyClass, bool>> predicate from Expression<Func<MyClass, string>>? 如何动态创建表达式<Func<MyClass, bool> &gt; 谓词? - How do I dynamically create an Expression<Func<MyClass, bool>> predicate? 如何动态创建表达式<func<myclass, bool> &gt; 带有字符串参数的谓词? </func<myclass,> - How do I dynamically create an Expression<Func<MyClass, bool>> predicate with string parameter? 我可以动态创建一个表达式 <Func<T, bool> &gt;谓词,但我如何创建表达式 <Func<T1,T2,bool> &gt; - I can dynamically create an Expression<Func<T, bool>> predicate ,but how do i create Expression<Func<T1,T2,bool>> 如何组合表达式 Expression <Func<T1, T2, bool> &gt; 到单个表达式<Func<T2, bool> &gt; - How to combine expressions Expression<Func<T1, T2, bool>> to a single Expression<Func<T2, bool>> 我怎么能做一个Func <object[],Expression<Func<T,bool> &gt;&gt;动态? - How can I do a Func<object[],Expression<Func<T,bool>>> dynamic? 组合表达式(Expression <Func<TIn,TOut> &gt; 带有表情<Func<TOut, bool> &gt;) - Combine Expression (Expression<Func<TIn,TOut>> with Expression<Func<TOut, bool>>) 如何包装Func <dynamic, MyClass> 属性 - How do I wrap Func<dynamic, MyClass> property 如何映射表达式 <Func<TEntity, bool> &gt;表达 <Func<TDbEntity, bool> &gt; - How to map Expression<Func<TEntity, bool>> to Expression<Func<TDbEntity, bool>> 如何提取传递给Expression的属性名称和值 <Func<T,bool> &gt;? - How do I extract the property name and value being passed into an Expression<Func<T,bool>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM