What Is Deferred/ Lazy Loading And Eager Loading?

In our previous article, I gave a brief introduction about LINQ. You can read about it here,

Now, in this article, we will go one step further and discuss when a LINQ query gets executed. LINQ query is not executed when constructed but when enumerated. There are two types of query executions in LINQ, which are given below. 

  1. Deferred or Lazy Loading
  2. Eager Loading.

We will discuss both one by one with an example. Most LINQ operators perform Deferred/Lazy Loading, except the conditions given below.

  1. Operator returns single element or a scalar value.
  2. Using following conversion operator ToArray, ToList, ToDictionary, ToLookup.

We will discuss both the conditions, when we discuss Eager Loading.

Deferred/Lazy Loading

In Lazy Loading, a query is not executed, until it is requested. Let’s understand this with an example.

Example

First create a list of an integer type.

  1. List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };   

Now, write a LINQ query to get an even number from the list.

  1. var result = number.Where(x => x % 2 == 0);   

Now, add one or more element to the list.

  1. number.Add(20);   

Now, we are traversing result, using foreach loop. 

  1. foreach (int item in result)  
  2. {  
  3.     Console.WriteLine(item);  
  4. }   

What will be the output?

Output will include 20

Output

Output

An output includes 20 because of Lazy Loading. LINQ query gets executed when foreach loop has started or when MoveNext is called on an Enumerator and an element 20 is added to the list before the execution.

I want to discuss one more example of deferred execution to better understand this. 

  1. List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  2. var result = number.Where(x => x % 2 == 0);  
  3. number.Add(20);  
  4. foreach (int item in result)  
  5. {  
  6.     Console.WriteLine(item + " is even number");  
  7. }  
  8. number.Clear();  
  9. Console.WriteLine("Total Element " + number.Count);  
  10.   
  11. foreach (int item in result)  
  12. {  
  13.     Console.WriteLine(item + " is even number");  
  14. }  

Output

Output

In this example, we first display even numbers from the collection, followed by clearing all the elements from the number array, then we will display total count, which is zero but when we again display even numbers, then it shows nothing. This is called Reevaluation. When we call our second loop, it again evaluates our LINQ query, as there are no elements in our array, so nothing is displayed.

Note

Please try the same problem with Eager Loading and see the results.

Eager Loading

In Eager Loading, LINQ query is executed when Conversion operator is called. To make a LINQ query to perform Eager Loading, use any condition given below.

  1. Operator returns single element or a scalar value.
  2. Using following conversion operator ToArray, ToList, ToDictionary, ToLookup

Example Condition 1 

  1. List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  2. var result = number.Where(x => x % 2 == 0).Count();  
  3. number.Add(20);  
  4. Console.WriteLine(result);   

Output

Output

In this, query is executed before number is added to the list.

Condition 2 

  1. List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  2. var result = number.Where(x => x % 2 == 0).ToArray();  
  3. number.Add(20);  
  4. foreach (int item in result)  
  5. {  
  6.     Console.WriteLine(item);  
  7. }   

Output

Output

Similarly this query result contains 2 4 6 8 not 20.

Note

In LINQ to an Entity, we can also use Include method to make Eager Loading.

Summary

In this article, we discussed two types of LINQ query execution techniques and how to convert Lazy Loading to Eager Loading.


Similar Articles