wizzardmr42
wizzardmr42

Reputation: 1644

Making expressions from lambda functions that are not for comparison

I'm currently building a UI that builds up queries and I would like to be able to store other expressions in the form of a lambda function (as it makes it easy to add them with intellisense etc). However, I can't find any way of getting eg. a lambda that performs a member access to be converted to an expression that I can then insert into an expression tree.

nb. it isn't just member accesses that I need

ie. I am trying to fill in the body of the following function

Function GetExpression(Of ParamType, ReturnType) _
    (f As Func(Of ParamType, ReturnType)) As Expression

Upvotes: 1

Views: 74

Answers (1)

Christian Hayter
Christian Hayter

Reputation: 31071

Change your method parameter to this:

Function GetExpression(Of ParamType, ReturnType) _
    (f As Expression(Of Func(Of ParamType, ReturnType))) As Expression

When you call this with a lambda, the compiler will supply the tokenised expression tree to the method instead of the compiled delegate.

Compare and contrast Enumerable.Where, which executes the lambda, with Queryable.Where, which doesn't.

Upvotes: 2

Related Questions