English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

LINQ Expression (Expression)

Lambda expressions can be assigned to Func or Action type delegates to process in-memory collections. The .NET compiler converts lambda expressions assigned to Func or Action type delegates into executable code at compile time.

LINQ introduces a new type called Expression, which represents strongly-typed lambda expressions. This means that lambda expressions can also be assigned to the Expression <TDelegate> type. The .NET compiler converts lambda expressions assigned to Expression <TDelegate> into expression trees rather than executable code. Remote LINQ query providers use this expression tree as a data structure to build runtime queries (such as LINQ-to-SQL, EntityFramework, or any other LINQ query provider that implements the IQueryable <T> interface).

The following figure illustrates the difference between assigning a lambda expression to a Func or Action delegate and an Expression in LINQ.

Expression and Func

In the next section, we will learn about expression trees, but first, let's see how to define and call expressions.

Define Expression

ReferenceSystem.Linq.ExpressionsNamespace, and use the Expression <TDelegate> class to define an Expression. Expression <TDelegate> requires delegate types Func or Action.

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

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 expression in VB.Net
Dim isTeenAger As Func(Of Student, Boolean) = Function(s) s.Age > 12 And s.Age < 20

Now, you can use Expression to wrap the Func delegate and convert the above Func type delegate to Expression as follows:

 Example: Define an expression in C# Expresson

Expression<Func<Student, bool>> isTeenAgerExpr = s => s.Age > 12 && s.Age < 20;

Example: Define an 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, if you do not return a value from the delegate, you can also wrap the Action <t> type delegate with Expression.

Example: Define an expression in C#

Expression<Action<Student>> printStudentName = s => Console.WriteLine(s.StudentName);

Example: Define an expression in VB.Net

Dim printStudentName As Expression<Action(Of Student) >= 
                                        Function(s) Console.WriteLine(s.StudentName);

Therefore, you can define the Expression <TDelegate> type. Now, let's see how to call the delegate wrapped by Expression <TDelegate>.

Call Expression (Expression)

You can call the delegate wrapped by Expression in the same way as a delegate, but you need to use the Compile() method first. Compile() returnsFuncorActiontypeof delegate, so you can call it like a delegate.

    Example: Call expression in C#

Expression<Func<Student, bool>> isTeenAgerExpr = s => s.Age > 12 && s.Age < 20;
//Compile the Expression using Compile method to call it as a delegate
Func<Student, bool>  isTeenAger = isTeenAgerExpr.Compile();
            
//Invoke
bool result = isTeenAger(new Student(){ StudentID = 1, StudentName = "Steve", Age = 20});
Dim isTeenAgerExpr As Expression(Of Func(Of Student, Boolean)) = 
                                                    Function(s) s.Age > 12 And s.Age < 20
'Use the compile method to compile it as a delegate call compile expression
Dim isTeenAger As Func(Of Student, Boolean) = isTeenAgerExpr.Compile()
Dim result = isTeenAger(New Student() With { .StudentID = 1, .StudentName = "Steve", .Age = 20)

In the next section, we will learn about expression trees in detail.