简体   繁体   English

通过为T引入一个常量,将表达式<Func <T,T2,bool >>转换为Expression <Func <T2,bool >>

[英]Convert Expression<Func<T, T2, bool>> to Expression<Func<T2, bool>> by introducing a constant for T

I have an expression in the format of Expression<Func<T, T2, bool>> that I need to convert into an expression on the format of Expression<Func<T2, bool>> by replacing the T in the first expression with a constant value. 我有一个Expression<Func<T, T2, bool>>格式的Expression<Func<T, T2, bool>> ,我需要通过将第一个表达式中的T替换为Expression<Func<T2, bool>>的格式转换为表达式恒定价值。

I need this to stay as an expression so I can't just Invoke the expression with a constant as the first parameter. 我需要将它作为表达式保留,所以我不能只使用常量作为第一个参数调用表达式。

I've looked at the other questions here about expression trees but I can't really find a solution to my problem. 我已经看过这里有关表达树的其他问题,但我无法找到解决问题的方法。 I suspect I have to walk the expression tree to introduce the constant and remove one parameter but I don't even know where to start at the moment. 我怀疑我必须走表达式树来引入常量并删除一个参数,但我甚至不知道从哪里开始。 :( :(

You can use Expression.Invoke to create a new lambda expression that calls the other: 您可以使用Expression.Invoke创建一个调用另一个的新lambda表达式:

static Expression<Func<T2, bool>> PartialApply<T, T2>(Expression<Func<T, T2, bool>> expr, T c)
{
    var param = Expression.Parameter(typeof(T2), null);
    return Expression.Lambda<Func<T2, bool>>(
        Expression.Invoke(expr, Expression.Constant(c), param), 
        param);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何组合表达式 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<T1,bool> &gt;表达 <Func<T2,bool> 动态 - Convert Expression<Func<T1,bool>> to Expression<Func<T2,bool> dynamically 在表达式中转换类型<Func<T1, bool> &gt; 表达<Func<T2, bool> &gt; (LINQ to SQL) - Convert type in Expression<Func<T1, bool>> to Expression<Func<T2, bool>> (LINQ to SQL) 如何转换表达式 <Func<T1, bool> &gt;表达 <Func<T2, bool> &gt; - How to convert Expression<Func<T1, bool>> to Expression<Func<T2, bool>> 转换表达式<func<t1, bool> &gt; 到表达式<func<t2, bool> &gt; </func<t2,></func<t1,> - Convert Expression<Func<T1, bool>> to Expression<Func<T2, bool>> 如何转换 Linq 表达式<func<object,object,bool> &gt; 表达<func<t1,t2,bool> &gt; </func<t1,t2,bool></func<object,object,bool> - How to convert Linq Expression<Func<object,object,bool>> to Expression<Func<T1,T2,bool>> 转换功能 <T1,bool> 到功能 <T2,bool> - Convert Func<T1,bool> to Func<T2,bool> 如何投射表达<Func<T1,bool> &gt; 表达<Func<T2,bool> &gt;? - How cast Expression<Func<T1,bool>> to Expression<Func<T2,bool>>? 有没有办法 map Func<t1, bool> 功能<t2, bool> ?</t2,></t1,> - Is there a way to map Func<T1, bool> to Func<T2, bool>? 我可以动态创建一个表达式 <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>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM