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. namespace DeferredVsImmediate_Query
  6. {
  7. public class Employee
  8. {
  9. public int Emp_ID { get; set; }
  10. public string Name { get; set; }
  11. public string Email { get; set; }
  12. public string Country { get; set; }
  13. }
  14. }
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. namespace DeferredVsImmediate_Query
  8. {
  9. public partial class Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. var Emp_List = new List<Employee>(
  14. new Employee[]
  15. {
  16. new Employee{Emp_ID=1, Name="Shambhu Sharma", Email="[email protected]", Country="India"},
  17. new Employee{Emp_ID=2, Name="Manu Khanna", Email="[email protected]", Country="India"},
  18. new Employee{Emp_ID=3, Name="Abhishek Nigam", Email="[email protected]", Country="USA"},
  19. new Employee{Emp_ID=4, Name="Yogesh Gupta", Email="[email protected]", Country="USA"},
  20. new Employee{Emp_ID=5, Name="Shweta Kashyap", Email="[email protected]", Country="India"},
  21. new Employee{Emp_ID=6, Name="Shraddha Gaur", Email="[email protected]", Country="India"},
  22. new Employee{Emp_ID=7, Name="Akhilesh Atwal", Email="[email protected]", Country="India"},
  23. new Employee{Emp_ID=6, Name="Mayank Dhulekar", Email="[email protected]", Country="USA"},
  24. new Employee{Emp_ID=7, Name="Saurabh Mehrotra", Email="[email protected]", Country="USA"},
  25. new Employee{Emp_ID=7, Name="Mehak Jain", Email="[email protected]", Country="India"},
  26. });
  27. var Result = from a in Emp_List
  28. where a.Country.Equals("India")
  29. select new { a.Name };
  30. foreach (var EMP in Result)
  31. Response.Write(EMP.Name + "</br>");
  32. }
  33. }
  34. }
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