Immediate Execution of LINQ Query
Immediate execution is the reverse of deferred execution. It forces the LINQ query to execute and gets the result immediately. The 'To' conversion operators execute the given query and give the result immediately.
Method Syntax
In the following example, ToList() extension method executes the query immediately and returns the result.
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 won't find any result as shown below:
Query Syntax doesn't support 'To' operators but can use ToList(), ToArray() or ToDictionary() for immediate execution as 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 see the result in the teenAgerStudents collection, as below: