Jul 15, 2011

Explaining the vague of Expression Trees


Expression trees are mainly used to generate query at run time. Expression trees represent the whole lambda expression. Combined with generics expression trees provide an important code re-usability for the project.

When I was reading a book about LINQ, which is LINQ in action. I find difficulty on understanding about the expression trees. Even I though what is their importance? Where do we use Expression trees? I searched for the SO for question if they are already asked; even I find a triple of question which related to question. But still I didn’t convince totally where the expression trees fit in an application.

Luckily, I come to the need of them and I come up to share my lucky experience for the readers. When I was developing for my project, I come to the need of quick search capability by its primary key in each data entry form. Before that I had completed designing the Generic repository for all of my data access layer entity using LINQ to SQL. However rather than the conventional repository method I developed with the generics which accepts a functor expression i.e.

Public Function Find(ByVal where As System.Func(Of T, Boolean)) As System.Collections.Generic.IEnumerable(Of T)

Implements IRepository(Of T).Find

Return repository.GetTable(Of T).Where(where)

End Function

I want a generic search method which receives an Id as an argument and finds the particular entity cross ponding with the Id where the entity type is defined at runtime. When I dig up for various methods and techniques I couldn’t find to my help. Finally the expression trees come to the rescue.

Public Function SearchbyId(ByVal id As Integer) As T

Dim entity = Expression.Parameter(GetType(T),entity")

Dim whereexpr = Expression.Lambda(Of Func(Of T, Boolean))( _

Expression.Equal(Expression.Property(entity, "Id"), _

Expression.Constant(id)), New ParameterExpression() {entity})

Return SingleorDefault(whereexpr.Compile())

End Function

  • · Expression.Equal(Expression.Property(entity, "Id"), Expression.Constant(id)) – This represents the actual expression equality of the passed Id with entity id which is the primary key.
  • · Expression.Lambda(--------) : This converts into lambda expression when the expression is compiled at runtime with the compile method i.e whereexpr.compile()

This method assumes you have named all your tables’ primary key as “ID” so that every time the method is called it compares the passed value with Id.


An expression tree builds a lambda expression with its various methods and property at runtime. So when you come to the need of building query at runtime the solution is to use expression trees. for its full description of its property and methods MSDN is you mentor for now lets us recap with MSDN illustration of Expression trees.

Expression trees represent language-level code in the form of data. The data is stored in a tree-shaped structure. Each node in the expression tree represents an expression, for example a method call or a binary operation such as x < y. The following illustration shows an example of an expression and its representation in the form of an expression tree. The different parts of the expression are color coded to match the corresponding expression tree node in the expression tree. The different types of the expression tree nodes are also shown.

<!--[if gte mso 9]> Normal 0 false false false EN-US X-NONE X-NONE

There was an error in this gadget