Manually constructing Expressions

9 01 2008

[ Please read Marlon’s blog post before reading my post 😉 ]

My friend Marlon posted about how to parse Expression given a Lambda expression something like,


But what if you want to construct the above Lambda expression manually 🙂

Yes, that is 100% possible! You can build your queries manually too 😀

Looking again at the query,


We can split into two parts

1) x

2) x.Name==”Marlon”

What do these mean?

1) x is the parameter and is of type Customer

2) The property Name is checked against the string Marlon which is nothing but the property Name’s value

To speak in “LINQ” language,

1) x is a ParameterExpression of type Customer

2) x.Name==”Marlon” is a MemberExpression whose,

a) left expression is the property Name, and

b) right expression is the ConstantExpression of value “Marlon”

There is nothing new here. if you look at how Marlon has parsed the tree, we could easily arrive to the above query construction technique

So, having understood the basics, now let us construct it 🙂

//construct the parameter which is x and it is of type Customer
ParameterExpression xParam = Expression.Parameter(typeof(Customer), "x");

//construct our MemberExpression whose
//left expression is x.Name, and
//right expression its value "Marlon"
MemberExpression leftExpr = MemberExpression.Property(xParam, "Name");
Expression rightExpr = Expression.Constant(customer.Name);

//construct our BinaryExpression which is x.Name=="Marlon"
BinaryExpression myExpr = MemberExpression.Equal(leftExpr, rightExpr);

//now build our Expression using the parameter and BinaryExpression
Expression<Func<Customer, bool>> lambdaExpr =
    Expression.Lambda<Func<Customer, bool>>
            new ParameterExpression[] { xParam }

//call our function and pass our expression which we constructed
customer_url = GetCustsomerUrl(lambdaExpr);

//display the returned value
Console.WriteLine("URL = {0}", customer_url);

Its nothing but parsing the expression tree in reverse 😀

You can download the sample here




2 responses

26 01 2008
Marlon Grech

This post is really brilliant it is helping me a lot dude….. Thanks so much for this…. 🙂

27 03 2009
Jerry Nixon

Pretty interesting. I guess Linq is doing this behind the scenes? It’s like coding with ascii codes instead of letters. Ha! 99.99% of the time I think I prefer the real lambda expression.

It’s academeically interesting to know how it works, but not practical unless you need it to be dynamic for some reason. And, now that I think about it, that’s pretty darn nice. A very elegant option.

Thanks for taking the time. I imagine most readers stop about half way, get an asprin, and move on to youtube 🙂 There’s not much easy about this wing of c# if you ask me. But invest the time and you get serious reward.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: