我有一个谓词Expression<Func<T1, bool>>
我需要用它作为谓词Expression<Func<T2, bool>>
使用T1
的财产T2
我试图考虑几种方法,可能使用Expression.Invoke
但我无法理解它。
以供参考:
class T2 {
public T1 T1;
}
And
Expression<Func<T1, bool>> ConvertPredicates(Expression<Func<T2, bool>> predicate) {
//what to do here...
}
预先非常感谢。
在考虑表达式树之前,尝试使用普通 lambda 找到解决方案。
你有一个谓词
Func<T1, bool> p1
并想要一个谓词
Func<T2, bool> p2 = (x => p1(x.T1));
您可以将其构建为表达式树,如下所示:
Expression<Func<T2, bool>> Convert(Expression<Func<T1, bool>> predicate)
{
var x = Expression.Parameter(typeof(T2), "x");
return Expression.Lambda<Func<T2, bool>>(
Expression.Invoke(predicate, Expression.PropertyOrField(x, "T1")), x);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)