The Evil Greebo
The Evil Greebo

Reputation: 7138

Get Object from Delegate Filter Expression Expression<Func<T,bool>>

Using lambda delegate Expression> - where my expression takes a Role object (POCO).

Looking to use that POCO Role object and map it to a data layer Role object with matching properties. To do that, I need to be able to get the Role object from the delegate.

Example:

public List<Role> FindAll(Expression<Func<Role, bool>> filter)

Calling this method like:

FindAll(r => r.Name == role.Name);

r is type Role, and within the FindAll function, I can see that filter has one parameter, as such:

enter image description here

Can I extract that object? And how?

I'm sure it MUST be doable, after all, linq does it internally all the time...

Upvotes: 2

Views: 1555

Answers (1)

Bryan Watts
Bryan Watts

Reputation: 45465

There are two roles here: r, which represents the filter parameter, and role, which is an object that is closed over by the lambda expression. I assume you mean you want a reference to the role object, since you already found the ParameterExpression which represents r.

That object will be a ConstantExpression whose type is Role, and it will be the value of the Expression property of the MemberAccessExpression which represents role.Name. That will be the right-hand side of the BinaryOperator expression representing the equality test, which serves as the Body of the lambda expression.

Is that what you need?

Upvotes: 2

Related Questions