Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Expression and Expression Trees

  Author : Shailendra Chauhan
Posted On : 12 Jul 2014
Total Views : 63,896   
Updated On : 26 Sep 2016
 

In .NET, Expression is an abstract class which contains static methods and inherited by various types (like ParameterExpression, MethodCallExpression, BinaryExpression etc.) to create expression tree nodes of specific types. Also all these expression-specific expression tree types are defined in System.Linq.Expressions namespace.

Expression Trees

Expression Trees was first introduced in C# 3.0 (Visual Studio 2008), where they were mainly used by LINQ providers. Expression trees represent code in a tree-like format, where each node is an expression (for example, a method call or a binary operation such as x < y). You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as well as the execution of LINQ queries in various databases and the creation of dynamic queries.

Creating Expression Trees

  1. Using Expression Lambda

    The easiest way to generate an expression tree is to create an instance of the Expression<T> type, where T is a delegate type, and assign a lambda expression to this instance. Let’s take a look at the code.

     // Create an expression using expression lambda
    Expression<Func<int, int, int>> expression = (num1, num2) => num1 + num2;
    
    // Compile the expression
    Func<int, int, int> compiledExpression = expression.Compile();
    
    // Execute the expression. 
    int result = compiledExpression(3, 4); //return 7
    

    In this example, the C# compiler generates the expression tree from the provided lambda expression as shown below:

    //Create the expression parameters
    ParameterExpression num1 = Expression.Parameter(typeof(int), "num1");
    ParameterExpression num2 = Expression.Parameter(typeof(int), "num2");
    
    //Create the expression parameters
    ParameterExpression[] parameters = new ParameterExpression[] { num1, num2 };
    
    //Create the expression body
    BinaryExpression body = Expression.Add(num1, num2);
    
    //Create the expression 
    Expression<Func<int, int, int>> expression = Expression.Lambda<Func<int, int, int>>(body, parameters);
    

    This looks much more complicated, but this is what actually happens when you supply a lambda expression to an expression tree.

  2. Using Expression Tree API

    Expression class is used to create expression trees by using the API. In .NET Framework 4, the expression trees API also supports assignments and control flow expressions such as loops, conditional blocks, and try-catch blocks. By using the API, you can create expression trees that are more complex than those that can be created from lambda expressions. Using API, above code can be re-written as:

    //Create the expression parameters
    ParameterExpression num1 = Expression.Parameter(typeof(int), "num1");
    ParameterExpression num2 = Expression.Parameter(typeof(int), "num2");
    
    //Create the expression parameters
    ParameterExpression[] parameters = new ParameterExpression[] { num1, num2 };
    
    //Create the expression body
    BinaryExpression body = Expression.Add(num1, num2);
    
    //Create the expression 
    Expression<Func<int, int, int>> expression = Expression.Lambda<Func<int, int, int>>(body, parameters);
    
    // Compile the expression
    Func<int, int, int> compiledExpression = expression.Compile();
    
    // Execute the expression. 
    int result = compiledExpression(3, 4); //return 7
    

Expression Tree Structure

The simple structure of an Expression<TDelegate> has four properties as given below:

  1. Body

    The body of the expression.

  2. Parameters

    The parameters of the lambda expression.

  3. NodeType

    The type of node in the tree

  4. Type

    The type of the expression.

IEnumerable<T> and IQueryable<T> and Expression Trees

In LINQ, a query expression is compiled to expression trees or to delegates, depending on the type that is applied to query result. The IEnumerable<T> type LINQ queries are compiled to delegates and IQueryable or IQueryable<T> queries are compiled to expression trees. In short, LINQ queries that execute in process are compiled into delegates while the queries that executes out of process are compiled into expression trees.

In LINQ, domain-specific queries (like LINQ to SQL, LINQ to Entity) are result into IQueryable<T> type. The C# and Visual Basic compilers compile these queries into code that builds an expression trees at runtime. Then query provider traverse the expression tree and translate it into a query language (like T-SQL) which is appropriate for that data source.

Consider the following LINQ to SQL query expression:

var query = from c in db.Customers
where c.City == "Delhi"
select new { c.City, c.CompanyName };

Here, variable query that is returned by this LINQ query is of type IQueryable. Here is the declaration of IQueryable:

public interface IQueryable : IEnumerable
{
 Type ElementType { get; }
 Expression Expression { get; }
 IQueryProvider Provider { get; }
}

As you can see, IQueryable contains a property of type Expression which holds the expression tree and represents data structure equivalent to the executable code found in a query expression. The IQueryProvider type property hold the LINQ provider (like LINQ to SQL, LINQ to Entity etc.) and based on LINQ provider query is translated into an appropriate query (like T-SQL query). The Type property represents the type of the element(s) that are returned when the expression tree is executed.

In this way, the above code is never actually executed inside your program. It is first translated into the following SQL statement and then executed on SQL Server side.

SELECT [t0].[City], [t0].[CompanyName]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[City] = @p0

Expression Trees vs Lambda Expressions

A common misunderstanding is that expression trees are identical to lambda expressions. But this is not true since you can also create and modify expression trees by using API methods i.e. without using lambda expression syntax at all.

Also, every lambda expression cannot be implicitly converted into an expression tree. Only expression lambda is implicitly converted an expression tree and statement lambda i.e. multi-line lambda cannot be implicitly converted into expression tree.

What do you think?

I hope you will enjoy Expression Tree while playing with LINQ. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
23 MAY
Xamarin Forms : Build Cross-platform Apps (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
5 MAY
ASP.NET MVC with Angular4 Development (Online)

06:30 AM-08:30 AM IST(+5.30 GMT)

Know More
29 APR
Xamarin Forms : Build Cross-platform Apps (Classroom)

5:00 PM-6:30 PM

Know More
29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

Know More
27 APR
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

Know More
22 APR
NodeJS Development (Classroom)

11:00 AM-12:30 PM

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

8 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

03:30 PM-05:30 PM

24 FEB
Angular 2 with Type Script (Online)

09:00 PM-11:00 PM IST(Mon,Wed,Fri)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+