Author : Rahul
Last Modified : 18-Jun-2021
Complexity : Beginner

Deferred VS Immediate Query Execution in LINQ


In this article, we will learn about deferred execution and Immediate execution.

Deferred Execution

In Deferred execution, the query is not executed at the time of declaration instead it is executed when the result is iterated inside foreach loop.

Suppose we have a list of employees and we want to get the list of employees whose salary is greater than 50000. The query and result are as follow.

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees where p.Emp_Salary > 50000 select p;
     
     //Display Linq Query Result
     Console.WriteLine("Employees having salary greater than 50000:");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine(item.Emp_Name);
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Ravi", Emp_Salary = 60000, Emp_Department = "Admin" });
     return employees;
   }
 }

Output

Employees having salary greater than 50000:
Rahul
Nitish
Ravi

In the above case, the query is executed in a deferred manner. Here query is executed when we iterate result_Linq into foreach loop. If we change the employee list before putting result_Linq into foreach loop, the result_Linq will be executed based on the updated list. It is shown as below:

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = from p in employees where p.Emp_Salary > 50000 select p;
     
     //Update employee list
     employees.Add(new Employee() { Emp_Name = "Shiv", Emp_Department = "IT", Emp_Salary = 75000 });
     employees.Add(new Employee() { Emp_Name = "Sagar", Emp_Department = "HR", Emp_Salary = 65000 });
     
     //Display Linq Query Result after employee list updated
     Console.WriteLine("Employees having salary greater than 50000:");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine(item.Emp_Name);
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Ravi", Emp_Salary = 60000, Emp_Department = "Admin" });
     return employees;
   }
 }

Output

Employees having salary greater than 50000:
Rahul
Nitish
Ravi
Shiv
Sagar

In the above case first, we write the query for employees having a salary greater than 50000. After that, we have added two more employees named Shiv and Sagar to the employees list. Now when we put result_Linq into foreach loop, the result is shown based on the complete employee list.

 

Immediate Execution

In immediate execution, the query is executed at the time of declaration.
Suppose we have a list of employees and we want to get the list of employees whose salary is greater than 50000. The query and result are as follow:

 public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = (from p in employees where p.Emp_Salary > 50000 select p).ToList();
     
     //Display Linq Query Result
     Console.WriteLine("Employees having salary greater than 50000:");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine(item.Emp_Name);
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Ravi", Emp_Salary = 60000, Emp_Department = "Admin" });
     return employees;
   }
 }

Output

Employees having salary greater than 50000:
Rahul
Nitish
Ravi

In the above case, the query is executed in an immediate manner. Here query is executed when we declare the query and put the result into result_Linq. If we change the employee list before putting result_Linq into foreach, the result is shown based on the original list because the query is already executed at the time of declaration and there will be no effect of employee list change. It is shown as below.

public class Employee
 {
   public string Emp_Name { get; set; }
   public string Emp_Department { get; set; }
   public int Emp_Salary { get; set; }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     List<Employee> employees = GetEmployeeList();
     
     //Linq Query
     var result_Linq = (from p in employees where p.Emp_Salary > 50000 select p).ToList();
     
     //Update employee list
     employees.Add(new Employee() { Emp_Name = "Shiv", Emp_Department = "IT", Emp_Salary = 75000 });
     employees.Add(new Employee() { Emp_Name = "Sagar", Emp_Department = "HR", Emp_Salary = 65000 });
     
     //Display Linq Query Result after employee list updated
     Console.WriteLine("Employees having salary greater than 50000:");
     foreach (Employee item in result_Linq)
     {
       Console.WriteLine(item.Emp_Name);
     }
     
     Console.Read();
   }
   
   /// <summary>
   /// Method to get employee list
   /// </summary>
   /// <returns></returns>
   private static List<Employee> GetEmployeeList()
   {
     List<Employee> employees = new List<Employee>();
     employees.Add(new Employee() { Emp_Name = "Rahul", Emp_Salary = 70000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Amit", Emp_Salary = 20000, Emp_Department = "HR" });
     employees.Add(new Employee() { Emp_Name = "Suresh", Emp_Salary = 40000, Emp_Department = "IT" });
     employees.Add(new Employee() { Emp_Name = "Nitish", Emp_Salary = 80000, Emp_Department = "Account" });
     employees.Add(new Employee() { Emp_Name = "Ravi", Emp_Salary = 60000, Emp_Department = "Admin" });
     return employees;
   }
 }

Output

Employees having salary greater than 50000:
Rahul
Nitish
Ravi

In the above case first, we write the query for employees having a salary greater than 50000. After that, we have added two more employees named Shiv and Sagar to the employees list. Now when we put result_Linq into foreach loop, the result is shown based on the original employee list there is no effect of adding a new employee into the employee list.