11

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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, I downloaded LinqKit from the link you gave and it works perfectly with Invoke and AsExpandable. I agree, very interesting articles.
Would have been good to show what syntax it works.
@Vladimir the thing is there was no such syntax in C# and you had to use 3rd party libraries with their own syntax or you could have implemented something on your own. Anyway it's a bit late to do anything regarding your comment, cause I haven't been using .NET for several years now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.