ExpressionTree 的函数或谓词
可以说我有:
Func<Customer,bool > a = (c) => c.fullName == "John";
现在我想转换为表达式树,有什么方法可以做到这一点?
我知道我可以从一开始就将它定义为表达式树,但我的情况有所不同,因为我必须首先连接一些 lambda 表达式,然后将其传递给采用表达式树的方法,这样做会导致编译时错误!
示例:
Func<Customer, bool> a = (c) => c.fullName == "John";
Func<Customer, bool> b = (c) => c.LastName == "Smith";
Func<Customer, bool> final = c => a(c) && b(c);
现在我想将 Final 传递给一个方法,该
ExpressionTree<Func<Customer,bool >>
方法会给出编译时错误,
提前致谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能那样做。
Func<...>
类型的变量是一个委托,它基本上就像一个指向包含编译代码的内存位置的指针> 为 lambda 表达式。 .NET 中没有将已编译的代码转回表达式树的功能。根据您尝试执行的操作,也许您可以应对不完整的解决方案:创建一个调用委托的表达式树。由于我对您想要将表达式树传递到的方法一无所知,因此我不知道这对您来说是否是一个可行的解决方案。
摘要:如果您想要所有表达式的完整表达式树,则需要确保它们从一开始就是表达式树。一旦将其编译为委托,表达式树就会丢失。
一旦您确定它们是表达式树,您就可以使用如下所示的方式组合它们:
当然,这使用
AndAlso
运算符 (&&
) ;您还可以使用OrElse
来表示||
等。You cannot do that. A variable of type
Func<...>
is a delegate, which is basically like a pointer to a memory location that contains the compiled code for the lambda expression. There is no functionality in .NET to turn already-compiled code back into an expression tree.Depending on what you are trying to do, maybe you can contend with an incomplete solution: create an expression tree that calls the delegates. Since I don’t know anything about the method to which you want to pass the expression tree, I have no idea whether this is at all a feasible solution for you.
Summary: If you want the complete expression tree of all the expressions, you need to make sure that they are expression trees right from the start. As soon as you compile it into a delegate, the expression tree is lost.
Once you’ve made sure that they are expression trees, you can combine them using something like the following:
Of course, this uses the
AndAlso
operator (&&
); you can also useOrElse
for||
etc.您可以从表达式转到函数,但反之则不行。
你可以这样做:
但没有办法走其他路。
You can go from Expression to a Func, but not the other way around.
You can do this:
But there's no way to go the other way.
关于您修改后的问题,我认为这会起作用:
Regarding your revised question, I think this will work:
这是我认为适合您的解决方案。我将
Func
转换为Expression>
。它基于 这篇文章 ElegantCode
在这个例子中我使用了一个 Func:
Here is a solution that I think will work for you. I converts a
Func<TInput, TOutput>
to aExpression<Func<TInput, TOutput>>
.It was based in this post from ElegantCode
In this example I used a Func: