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

LINQ Immediate Execution Query

Immediate execution is the opposite of deferred execution. It forces the LINQ query to execute and immediately obtain the results. The "To" conversion operator executes the given query and immediately provides the results.

Method Syntax

In the following example, the ToList() extension method immediately executes the query and returns the results.

 C#: Immediate Execution

IList<Student> teenAgerStudents = 
                studentList.Where(s => s.age > 12 && s.age < 20).ToList();

 VB.Net: Immediate Execution

Dim teenAgerStudents As IList(Of Student) = 
                    studentList.Where(Function(s) s.Age > 12 And s.Age < 20).ToList()

Query Syntax

C#:
var teenAgerStudents = from s in studentList
                where s.age > 12 && s.age < 20
                select s;

The above query will not execute immediately. You will not find any results, as shown below:

Immediate Execution

Query syntax does not support the "To" operator, but you can use ToList(), ToArray(), or ToDictionary() to immediately execute, as shown below:

C#:
IList<Student> teenAgerStudents = (from s in studentList
                where s.age > 12 && s.age < 20
                select s).ToList();
VB.Net:
Dim teenAgerStudents As IList(Of Student) = (From s In studentList _
                Where s.Age > 12 And s.Age < 20 _
                Select s).ToList()

You can view the results in the teenAgerStudents collection as follows:

Immediate Execution