Back to: LINQ Tutorial For Beginners and Professionals
Deferred Execution vs Immediate Execution in LINQ
In this article, I am going to discuss the difference between LINQ Deferred Execution vs Immediate Execution in C# with Examples. Please read our previous article where we discussed the LINQ Zip Method in C# with an Example. The LINQ queries are executed in two different ways as follows.
- LINQ Deferred Execution
- LINQ Immediate Execution
Based on the above two types of Execution, the LINQ operators are divided into 2 categories. They are as follows:
- Deferred or Lazy Operators: These Query Operators are used for Deferred Execution. For example – Select, SelectMany, Where, Take, Skip, etc. belongs to the Deferred or Lazy Operators Category.
- Immediate or Greedy Operators: These Query Operators are used for Immediate Execution. For example – Count, Average, Min, Max, First, Last, ToArray, ToList, etc. belongs to the Immediate or Greedy Operators category.
LINQ Deferred Execution using C#:
In this case, the LINQ Query is not executed at the point of its declaration. That means, when we write a LINQ query, it doesn’t execute by itself. It executes only when we access the query results. So, here the execution of the query is deferred until the query variable is iterated over using for each loop. Let us understand this with an example. The below example is self-explained. So, please go through the comment lines.
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { public class Employee { public int ID { get; set; } public string Name { get; set; } public int Salary { get; set; } } class Program { public static void Main() { List<Employee> listEmployees = new List<Employee> { new Employee { ID= 1001, Name = "Priyanka", Salary = 80000 }, new Employee { ID= 1002, Name = "Anurag", Salary = 90000 }, new Employee { ID= 1003, Name = "Preety", Salary = 80000 } }; // In the below statement the LINQ Query is only defined and not executed // If the query is executed here, then the result should not display Santosh IEnumerable<Employee> result = from emp in listEmployees where emp.Salary == 80000 select emp; // Adding a new employee with Salary = 80000 to the collection listEmployees listEmployees.Add(new Employee { ID = 1004, Name = "Santosh", Salary = 80000 }); // The LINQ query is actually executed when we iterate thru using a for each loop // This is proved because Santosh is also included in the result foreach (Employee emp in result) { Console.WriteLine($" {emp.ID} {emp.Name} {emp.Salary}"); } Console.ReadKey(); } } }
Output:
Advantages of LINQ Deferred Execution in C#:
We will get the following advantages
- It avoids unnecessary query execution which improves the performance of the application.
- The Query creation and the Query execution are decoupled which provides us the flexibility to create the query in several steps.
- A LINQ deferred execution query is always re-evaluated when we re-enumerate. As a result, we always get updated data.
LINQ Immediate Execution using C#
In the case of Immediate Execution, the LINQ query is executed at the point of its declaration. So, it forces the query to execute and gets the result immediately. Let us see an example for a better understanding. The following example is self-explained. So, please go through the comment lines.
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { public class Employee { public int ID { get; set; } public string Name { get; set; } public int Salary { get; set; } } class Program { public static void Main() { List<Employee> listEmployees = new List<Employee> { new Employee { ID= 1001, Name = "Priyanka", Salary = 80000 }, new Employee { ID= 1002, Name = "Anurag", Salary = 90000 }, new Employee { ID= 1003, Name = "Preety", Salary = 80000 } }; // In the following statement, the LINQ Query is executed immediately as we are // Using the ToList() method which is a greedy operator which forces the query // to be executed immediately IEnumerable<Employee> result = (from emp in listEmployees where emp.Salary == 80000 select emp).ToList(); // Adding a new employee with Salary = 80000 to the collection listEmployees // will not have any effect on the result as the query is already executed listEmployees.Add(new Employee { ID = 1004, Name = "Santosh", Salary = 80000 }); // The above LINQ query is executed at the time of its creation. // This is proved because Santosh is not included in the result foreach (Employee emp in result) { Console.WriteLine($" {emp.ID} {emp.Name} {emp.Salary}"); } Console.ReadKey(); } } }
Output:
Example:
In the following Immediate Execution example, we are using the greedy operator which returns a single value.
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { public class Employee { public int ID { get; set; } public string Name { get; set; } public int Salary { get; set; } } class Program { public static void Main() { List<Employee> listEmployees = new List<Employee> { new Employee { ID= 1001, Name = "Priyanka", Salary = 80000 }, new Employee { ID= 1002, Name = "Anurag", Salary = 90000 }, new Employee { ID= 1003, Name = "Preety", Salary = 80000 } }; // In the following statement, the LINQ Query is executed immediately as we are // Using the Count() method which is a greedy operator which forces the query // to be executed immediately var result = (from emp in listEmployees where emp.Salary == 80000 select emp).Count(); // Adding a new employee with Salary = 80000 to the collection listEmployees // will not have any effect on the result as the query is already executed listEmployees.Add(new Employee { ID = 1004, Name = "Santosh", Salary = 80000 }); // The LINQ query is executed at the time of its creation. // This is proved because Santosh is not included in the count Console.WriteLine($" Employees with Salary 80000 : {result}"); Console.ReadKey(); } } }
Output:
That’s it for today. In this article, I try to explain the difference between LINQ Deferred Execution vs Immediate Execution using C# with Examples. I hope you understood the LINQ Deferred Execution vs Immediate Execution using C#. In the next article, I am going to discuss the ToList and ToArray Methods in C# with Examples.