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.
- Deferred or Lazy Loading
- Eager Loading.
We will discuss both one by one with an example. Most LINQ operators perform Deferred/Lazy Loading, except the conditions given below.
- Operator returns single element or a scalar value.
- 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.
- 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.
- var result = number.Where(x => x % 2 == 0);
Now, add one or more element to the list.
Now, we are traversing result, using foreach loop.
- foreach (int item in result)
- {
- Console.WriteLine(item);
- }
What will be the output?
Output will include 20
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.
- List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- var result = number.Where(x => x % 2 == 0);
- number.Add(20);
- foreach (int item in result)
- {
- Console.WriteLine(item + " is even number");
- }
- number.Clear();
- Console.WriteLine("Total Element " + number.Count);
-
- foreach (int item in result)
- {
- Console.WriteLine(item + " is even number");
- }
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.
- Operator returns single element or a scalar value.
- Using following conversion operator ToArray, ToList, ToDictionary, ToLookup
Example Condition 1
- List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- var result = number.Where(x => x % 2 == 0).Count();
- number.Add(20);
- Console.WriteLine(result);
Output
In this, query is executed before number is added to the list.
Condition 2
- List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- var result = number.Where(x => x % 2 == 0).ToArray();
- number.Add(20);
- foreach (int item in result)
- {
- Console.WriteLine(item);
- }
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.