Thursday, May 3, 2018

Deferred Execution or Delayed Execution in LINQ Query

Deferred execution means that the evaluation of an expression is delayed until it is required. It improves performance.
Deferred execution is applicaion to any in memory collection like LINQ-to-SQL, LINQ-to-Entities,LINQ-to-XML, etc.

Example :-

1. IList studenlist = new List(){new Student(){Id=1,Name="Abdul",new Student(){Id=2,Name="Ahad"}

2. var teenAgerStudens = from s in studentlist
       where id>0
select s;

3.foreach(Student teenStudent in teenAgerStudens)
Console.WriteLine("Student Name {0}", teenStudent.Name)

The will not execute at line 2 where it is delared but will execute at line 3 where
it is used.

No comments: