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