转换表达式来; Func键< T,T2,布尔>>到表达式来; Func键< T2,布尔>>通过引入一个常数对于T [英] Convert Expression<Func<T, T2, bool>> to Expression<Func<T2, bool>> by introducing a constant for T

查看:171
本文介绍了转换表达式来; Func键< T,T2,布尔>>到表达式来; Func键< T2,布尔>>通过引入一个常数对于T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表达式来的格式表达; Func键< T,T2,布尔>> ,我需要转换为表达对格式表达式来; Func键< T2,布尔>>

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.

我这需要留下来作为一种表达,所以我不能只调用一个常量作为第一个参数的表达。

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. :(

推荐答案

您可以使用的 Expression.Invoke 来创建调用其他新的lambda表达式:

You can use Expression.Invoke to create a new lambda expression that calls the other:

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);
}

这篇关于转换表达式来; Func键&LT; T,T2,布尔&GT;&GT;到表达式来; Func键&LT; T2,布尔&GT;&GT;通过引入一个常数对于T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆