Expression in LINQ

The lambda Expression can be assigned to the Func or Action type delegates to process over in-memory collections. The .NET compiler converts the lambda expression assigned to Func or Action type delegate into executable code at compile time.

LINQ introduced the new type called Expression that represents strongly typed lambda expression. It means lambda expression can also be assigned to Expression<TDelegate> type. The .NET compiler converts the lambda expression which is assigned to Expression<TDelegate> into an Expression tree instead of executable code. This expression tree is used by remote LINQ query providers as a data structure to build a runtime query out of it (such as LINQ-to-SQL, EntityFramework or any other LINQ query provider that implements IQueryable<T> interface).

The following figure illustrates differences when the lambda expression assigned to the Func or Action delegate and the Expression in LINQ.

ExpressionTree Process
Expression and Func

We will learn Expression tree in the next section but first, let's see how to define and invoke an Expression.

Define an Expression

Take the reference of System.Linq.Expressions namespace and use an Expression<TDelegate> class to define an Expression. Expression<TDelegate> requires delegate type Func or Action.

For example, you can assign lambda expression to the isTeenAger variable of Func type delegate, as shown below:

Example: Define Func delegate for an expression in C#
public class Student 
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int Age { get; set; }
}

Func<Student, bool> isTeenAger = s => s.Age > 12 && s.Age < 20;
Example: Define Func delegate for an expression in VB.Net
Dim isTeenAger As Func(Of Student, Boolean) = Function(s) s.Age > 12 And s.Age < 20

And now, you can convert the above Func type delegate into an Expression by wrapping Func delegate with Expresson, as below:

Example: Define Expression in C#
Expression<Func<Student, bool>> isTeenAgerExpr = s => s.Age > 12 && s.Age < 20;
Example: Define Expression in VB.Net
Dim isTeenAgerExpr As Expression(Func(Of Student, Boolean)) = 
                                        Function(s) s.Age > 12 And s.Age < 20

in the same way, you can also wrap an Action<t> type delegate with Expression if you don't return a value from the delegate.

Example: Define Expression in C#
Expression<Action<Student>> printStudentName = s => Console.WriteLine(s.StudentName);
Example: Define Expression in VB.Net
Dim printStudentName As Expression(Action(Of Student) = 
                                        Function(s) Console.WriteLine(s.StudentName);

Thus, you can define Expression<TDelegate> type. Now, let's see how to invoke delegate wrapped by an Expression<TDelegate>.

Invoke an Expression

You can invoke the delegate wrapped by an Expression the same way as a delegate, but first you need to compile it using the Compile() method. Compile() returns delegateof Func or Action type so that you can invoke it like a delegate.

Example: Invoke Expression in C#
Expression<Func<Student, bool>> isTeenAgerExpr = s => s.Age > 12 && s.Age < 20;

//compile Expression using Compile method to invoke it as Delegate
Func<Student, bool>  isTeenAger = isTeenAgerExpr.Compile();
            
//Invoke
bool result = isTeenAger(new Student(){ StudentID = 1, StudentName = "Steve", Age = 20});
Example: Invoke Expression in VB.Net
Dim isTeenAgerExpr As Expression(Of Func(Of Student, Boolean)) = 
                                                    Function(s) s.Age > 12 And s.Age < 20

'compile Expression using Compile method to invoke it as Delegate
Dim isTeenAger As Func(Of Student, Boolean) = isTeenAgerExpr.Compile()

Dim result = isTeenAger(New Student() With { .StudentID = 1, .StudentName = "Steve", .Age = 20})

Learn about the Expression tree in detail in the next section.

Want to check how much you know LINQ?