Deferred Vs Immediate Query Execution in LINQ

In Deferred Execution, the query is not executed when declared. It is executed when the query object is iterated over a loop.

In Immediate Execution, the query is executed when it is declared.

Deferred Execution:

Now we will see both the executions using an example. For this I created a Visual Studio Solution. Here in the solution I added a new Class Employee as in the following screenshot:

Employee
Figure 1: Class Employee

Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace DeferredVsImmediate_Query  
  7. {  
  8.     public class Employee  
  9.     {  
  10.         public int Emp_ID { getset; }  
  11.         public string Name { getset; }  
  12.         public string Email { getset; }  
  13.         public string Country { getset; }  
  14.     }  
  15. }  
Add a New Web Page Default.aspx and add the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace DeferredVsImmediate_Query  
  9. {  
  10.     public partial class Default : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             var Emp_List = new List<Employee>(  
  15.                 new Employee[]  
  16.                 {  
  17.                     new Employee{Emp_ID=1, Name="Shambhu Sharma", Email="shambhu@shambhu.com", Country="India"},  
  18.                     new Employee{Emp_ID=2, Name="Manu Khanna", Email="manu@manu.com", Country="India"},  
  19.                     new Employee{Emp_ID=3, Name="Abhishek Nigam", Email="abhishek@abhishek.com", Country="USA"},  
  20.                     new Employee{Emp_ID=4, Name="Yogesh Gupta", Email="yogesh@yogesh.com", Country="USA"},  
  21.                     new Employee{Emp_ID=5, Name="Shweta Kashyap", Email="Shweta@Shweta.com", Country="India"},  
  22.                     new Employee{Emp_ID=6, Name="Shraddha Gaur", Email="Shraddha@Shraddha.com", Country="India"},  
  23.                     new Employee{Emp_ID=7, Name="Akhilesh Atwal", Email="Akhilesh@Akhilesh.com", Country="India"},  
  24.                     new Employee{Emp_ID=6, Name="Mayank Dhulekar", Email="Mayank@Mayank.com", Country="USA"},  
  25.                     new Employee{Emp_ID=7, Name="Saurabh Mehrotra", Email="Saurabh@Saurabh.com", Country="USA"},  
  26.                     new Employee{Emp_ID=7, Name="Mehak Jain", Email="Mehak@Mehak.com", Country="India"},  
  27.                 });  
  28.   
  29.             var Result = from a in Emp_List  
  30.                          where a.Country.Equals("India")  
  31.                          select new { a.Name };  
  32.   
  33.             foreach (var EMP in Result)  
  34.                 Response.Write(EMP.Name + "</br>");  
  35.         }  
  36.     }  
  37. }  
Now run your application:

Now Run
Figure 2: Output

Now you can see the following when your query executes:

see when my query executed
Figure 3: Query Execution

Now add a new record after your query to see the deferred execution:

Add a new record after your query
Figure 4: Deferred Execution 

Now run your app:

Now run your app
Figure 5: Output

Immediate Execution: We can force our query to execute immediately as in the following:

query to execute
Figure 6: Immediate Execution

Now run your application:

run your application
Figure 7: Output

Now let us see both the executions:

code
Figure 8: Both the executions