tags:

views:

223

answers:

5

I would like to use the lambda expression in my Repository as a generic parameter. If I use a firm like this one:

MyEntity entity:null

void Run(Expression<Func<MyEntity ,bool>> expression)

I can call it in this way:

Run(x => x.FirstName = "Whatever")

What I would like is the ability to do something like this:

Run(x => x.FirstName = "Whatever" and x.LastName = "whatelse")

or

Run(x => x.FirstName = "Whatever" && x.LastName = "whatelse")

And read the content of x.FirstName and x.LastName inside the procedure Run. Is there a way to accomplish that?

+3  A: 

You have your Run method declared as:

void Run(Expression<Func<MyEntity ,bool>> expression)

This would just work if you just declared it as:

void Run(Func<MyEntity ,bool> expression)

Is there a reason you're taking an expression, and not just a delegate (Func<MyEntity,bool>) directly?

Reed Copsey
Reed, I imagine the poster is using Expressions to be able to inspect the expression within the Run method (i.e., not just execute it).
David Andres
Is there a difference?
A: 

Have you tried the following:

Run(x => (x.FirstName == "Whatever" && x.LastName == "whatelse"))
LeBleu
Yes it works but I want to know the value of x.FirstName inside the procedure run, how I can do that?
A: 

Exactly I need to read the Expression inside my Run method. So I would like to evaluate the context of x.FirstName, for example, inside the Run method. I know it's possible but I cannot figure out how.

A: 

I think you need to use

void Run(Action<MyEntity> action)

and then

Run(x => {x.FirstName = "Whatever"; x.LastName = "whatelse"})

since I don't see that you use return value.

Not sure if Expression<Action<MyEntity>> will work (if you need to inspect it) but you can try.

queen3
Yes but how I can read then the Action<MyEntity> assigned values?
Why don't you use this: { MyEntity tmp; action(tmp); if (tmp.FirstName)...} - if you don't want to affect your "working" entity.
queen3
Here's a link to example code of how you can parse Expression[Action]: http://stackoverflow.com/questions/717091/reflection-get-the-list-of-method-calls-inside-a-lambda-expression/717332#717332
queen3
I want to call this expression and fill with lamba expression the entity contained in the parameterThen I want to evaluate the properties of the entity in the run procedure
A: 

All, the reason why the OP is passing in an Expression<Func<MyEntity, bool>> parameter as opposed to a Func<MyEntity, bool> parameter is that he/she wants to inspect the expression itself within the Run method.

The Expression object has several properties that enable you to inspect the peculiars of the expression at runtime. One of these, Body, can be used to discern the code used within the expression, while the Parameters collection can be used to inspect the type and order of parameters that can be passed to the expression when compiled.

Console.WriteLine(expression.Parameters[0].Type); //writes MyEntity
Console.WriteLine(expression.Body); 
   //writes ((x.FirstName == "Bob" && x.LastName == "Smith"))

In your example, you are defining an expression but you haven't actually called it. Therefore, you haven't actually passed in a parameter (the MyEntity object) and so there aren't any arguments to inspect.

David Andres