How to: Use Expression Trees to Build Dynamic Queries

This topic describes how to use expression trees to create dynamic LINQ queries. Dynamic queries are useful when the specifics of a query are not known at compile time. For example, an application might provide a user interface that enables the end user to specify one or more predicates to filter the data. In order to use LINQ for querying, this kind of application must use expression trees to create the LINQ query at runtime.

The following example shows you how to use expression trees to construct a query against an IQueryable data source and then execute it. The code builds an expression tree to represent the following query:

C# Query

companies.Where(company => (company.ToLower() == "coho winery" || company.Length > 16)).OrderBy(company => company)

Visual Basic Query

companies.Where(Function(company) company.ToLower() = "coho winery" OrElse company.Length > 16).OrderBy(Function(company) company)

The factory methods in the System.Linq.Expressions namespace are used to create expression trees that represent the expressions that make up the overall query. The expressions that represent calls to the standard query operator methods refer to the Queryable implementations of these methods. The final expression tree is passed to the CreateQuery(Of TElement)(Expression) implementation of the provider of the IQueryable data source to create an executable query of type IQueryable. The results are obtained by enumerating that query variable.

Dim companies() As String = _
    {"Consolidated Messenger", "Alpine Ski House", "Southridge Video", "City Power & Light", _
                       "Coho Winery", "Wide World Importers", "Graphic Design Institute", "Adventure Works", _
                       "Humongous Insurance", "Woodgrove Bank", "Margie's Travel", "Northwind Traders", _
                       "Blue Yonder Airlines", "Trey Research", "The Phone Company", _
                       "Wingtip Toys", "Lucerne Publishing", "Fourth Coffee"}

' The IQueryable data to query. 
Dim queryableData As IQueryable(Of String) = companies.AsQueryable()

' Compose the expression tree that represents the parameter to the predicate. 
Dim pe As ParameterExpression = Expression.Parameter(GetType(String), "company")

' ***** Where(Function(company) company.ToLower() = "coho winery" OrElse company.Length > 16) ***** 
' Create an expression tree that represents the expression: company.ToLower() = "coho winery". 
Dim left As Expression = Expression.Call(pe, GetType(String).GetMethod("ToLower", System.Type.EmptyTypes))
Dim right As Expression = Expression.Constant("coho winery")
Dim e1 As Expression = Expression.Equal(left, right)

' Create an expression tree that represents the expression: company.Length > 16.
left = Expression.Property(pe, GetType(String).GetProperty("Length"))
right = Expression.Constant(16, GetType(Integer))
Dim e2 As Expression = Expression.GreaterThan(left, right)

' Combine the expressions to create an expression tree that represents the 
' expression: company.ToLower() = "coho winery" OrElse company.Length > 16). 
Dim predicateBody As Expression = Expression.OrElse(e1, e2)

' Create an expression tree that represents the expression: 
' queryableData.Where(Function(company) company.ToLower() = "coho winery" OrElse company.Length > 16) 
Dim whereCallExpression As MethodCallExpression = Expression.Call( _
        GetType(Queryable), _
        "Where", _
        New Type() {queryableData.ElementType}, _
        queryableData.Expression, _
        Expression.Lambda(Of Func(Of String, Boolean))(predicateBody, New ParameterExpression() {pe}))
' ***** End Where ***** 

' ***** OrderBy(Function(company) company) ***** 
' Create an expression tree that represents the expression: 
' whereCallExpression.OrderBy(Function(company) company) 
Dim orderByCallExpression As MethodCallExpression = Expression.Call( _
        GetType(Queryable), _
        "OrderBy", _
        New Type() {queryableData.ElementType, queryableData.ElementType}, _
        whereCallExpression, _
        Expression.Lambda(Of Func(Of String, String))(pe, New ParameterExpression() {pe}))
' ***** End OrderBy ***** 

' Create an executable query from the expression tree. 
Dim results As IQueryable(Of String) = queryableData.Provider.CreateQuery(Of String)(orderByCallExpression)

' Enumerate the results. 
For Each company As String In results

' This code produces the following output: 

' Blue Yonder Airlines 
' City Power & Light 
' Coho Winery 
' Consolidated Messenger 
' Graphic Design Institute 
' Humongous Insurance 
' Lucerne Publishing 
' Northwind Traders 
' The Phone Company 
' Wide World Importers

This code uses a fixed number of expressions in the predicate that is passed to the Queryable.Where method. However, you can write an application that combines a variable number of predicate expressions that depends on the user input. You can also vary the standard query operators that are called in the query, depending on the input from the user.

  • Create a new Console Application project in Visual Studio.

  • Add a reference to System.Core.dll if it is not already referenced.

  • Include the System.Linq.Expressions namespace.

  • Copy the code from the example and paste it into the Main method (C#) or the Main Sub procedure (Visual Basic).

Community Additions