Evaluate C# expression inside another expression

試著忘記壹切 提交于 2020-01-13 07:58:07

问题


I want to use an expression in another one:

Expression<Func<double, double>> f = x => x * x * 27 + blah ... expression with x;

Expression<Func<double, double>> g = y => 3 + 8 * f.Compile()(y) * y * blah... expression with y and f(y);

This will not work when sent to LINQ to SQL because f.Compile() is unknown to SQL.

How do you evaluate the expression f on the variable y without compiling it, but still using normal syntax to define g?

I don't want to have to define all of g with some unreadable Expression.Add/Expression.Multiply etc. statements.

Thanks.


回答1:


Take a look at Calling functions in LINQ queries and LINQ Extensions project. CLinq part is irrelevant to your question, but it also includes LinqExt library, which is just what are you looking for. The same approach is also used by LinqKit which also provides other useful extensions for Linq.



来源:https://stackoverflow.com/questions/6977890/evaluate-c-sharp-expression-inside-another-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!