Lambda expressions and lambda expression trees

Lately I had to dive into consequences for a migration from VS 2005 to VS 2008 and I tumbled over lambda expressions and lambda expression trees. Although lambda expressions themselves are quite easy to understand, I had a hard time trying to understand lambda expression trees.

However, I found a very sharp explanation on lambda expression trees at Charlie Calvert’s blog. It helped me a lot getting the right point of view.

In fact, expression trees are not that difficult to understand. It is the people’s stories which make them hard to understand. One of the mistakes made in relation to expression trees is that they are extendable. Simple: they are not. The only difference is that lambda expressions are compiled at compile time, lambda expression trees not (they are stored tree-wise, as shown below – that explains their name).

Lambda expression tree

The reason expression trees have an added value lies in the IQueryable<T> interface. As stated by Charlie, that interface has an Expression property. When using the interface, e.g. with LINQ to SQL, the expression tree is generated for the LINQ query, but when it has to be executed it can be translated into the target language (e.g. T-SQL) from the lambda expression tree.

Technorati tags: