Take the 2-minute tour ×

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.


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...

share|improve this question
Why are you duplicating Where? –  leppie Feb 1 '12 at 16:38
Have you tried casting them to the specialized Expression's? –  leppie Feb 1 '12 at 16:39
@leppie 1) What do you mean, 'Why am I duplicating Where? 2) Parameter[0] is of type ParameterExpression - so far I haven't found any properties off of that I can cast to my own type from, but I'm still trying. –  The Evil Greebo Feb 1 '12 at 16:47
The debugger is listing all the types for you already. Just cast to that, and viola, you have the properties you see in the debugger. It is a fairly trivial task, no magic involved :) –  leppie Feb 1 '12 at 16:52
@TheEvilGreebo Why can’t you cast that, exactly? –  Konrad Rudolph Feb 1 '12 at 16:59

1 Answer 1

up vote 2 down vote accepted

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?

share|improve this answer
Yes and no. No, it's not what I really wanted - but reading your explanation has helped crystallize why it won't work. (r=>r.name = role.name) is only one type of possible filters. I could just as easily say (r=>r.name="Fred") - which would never have a role object so of course, why would that object be there... –  The Evil Greebo Feb 1 '12 at 17:34

Your Answer


