Dynamic Query Using LINQ

This article explains how to create a dynamic query in LINQ. In order to do this, we first need to understand the concept of expression trees.

Expression trees

As per the MSDN: Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y

In simple language, an expression tree is a tree data structure that can be used to represent some executable piece of code, in the form of nodes of this tree. This code is then converted into actual executable code. This kind of property allows you to generate the dynamic LINQ query. So let's start with the code.

We have the following LINQ query that returns the data of a Customer, based on the CustomerId.

  1. _dbEntities.Customers.Where(cust => cust.CustomerId == 10).FirstOrDefault(); 

We will now convert this query into a dynamic query, by creating an expression tree for it. First, we need to add the namespace System.Linq.Expressions. This class contains various static methods, that we will use to generate the query. These methods include Parameter, Constant, Property and so on. We will be using these methods to generate the query, in multiple steps. So let's start.

Step 1

We need to first generate the input parameter represented by code on the left of the lamda operator, in other words cust =>. For this, we use the Expression.Parameter method, that will be passed 2 parameters Type and String name. Here:

  • Type is the type of entity we are using, in other words Customer entity.
  • The string is any name we use to represent an input to the parameter, that in our case is cust. So our code will be:
  1. ParameterExpression pe = Expression.Parameter(Customer, "cust"); 

Step 2

Next, we need to use the CustomerId property to make the comparison with a value that is represented by cust.CustomerId in our initial query. So we need to get that property first and we use the Expression.Property method for it. So our code will be:

  1. var _prpToUse = Expression.Property(pe, "CustomerId"); 

Step 3

Now we need to compare our CustomerId with some value say 10 in our case. So we generate our expression for this using the Expression.Constant method as:

  1. var _cnstToUse = Expression.Constant(10); 

Step 4

Next we need to combine the preceding two expressions, to generate the expression cust.CustomerId == 10. For this purpose, we will use the Expression.Equal method. So our code becomes:

  1. var qry = Expression.Equal(_prpToUse, _cnstToUse); 

Step 5

Now, we have the expression of the form cust => cust.CustomerId == 10 and need to combine it with the Where extension method, to complete the query. So the following code will pass the preceding expression to it, using the MethodCallExpression method.

  1. MethodCallExpression whereExpression = Expression.Call(  
  2. typeof(Queryable),  
  3. "Where",  
  4. new Type[] { lst.ElementType },  
  5. lst.Expression,  
  6. Expression.Lambda<Func<Customer, bool>>(qry, new ParameterExpression[] { pe })); 

Step 6

Finally, we need to execute the query using the CreateQuery method in the provider on which we need to execute the query.

  1. lstData.Provider.CreateQuery<Customer>(whereExpression).FirstOrDefault(); 

Here, lstData is the list of customers that we have from the database. Execute the code and see the results. It will be the same that we had at the start of the article.

In all the preceding steps, except Step 6, we have created expressions of code that are combined to create an Expression tree, as a whole, which is nothing but the query that we executed at the start of the article.

The following is the generic implementation of the code that we created:



So this was about the use of Expression Trees to generate a dynamic LINQ query. I hope you enjoyed reading it.

Up Next
    Ebook Download
    View all
    Learn
    View all