问题
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