Linq: Discussing Deferred and Immediate Query Execution
These days Linq is getting more fame, I like and recommend it as a developer. In this shorp discussion, I am not going to explain Linq.
So, Linq is having different approches to write queries, but having two different behaviors of query execution, i.e. Deferred and Immediate execution.
In this short discussion, we will discuss this with a simple code example.
Abstract
These days Linq is getting more fame, I like and recommend it as a developer. In this shorp discussion, I am not going to explain Linq.
So, Linq is having different approches to write queries, but having two different behaviors of query execution, i.e. Deferred and Immediate execution.
In this short discussion, we will discuss this with a simple code example.Introduction
Linq is having different approches to write queries, but having two different behaviors of query execution, i.e. Deferred and Immediate execution.
In this short discussion, we will discuss this with a simple code example.Pre-requisite
To understand the code exmaple and Linq behaviors for query execution, should know basics of Linq. Basic ideas of LinqPad, in this discussion
I used LinqPad, to demostarte our example.Lets start
Consider following example and think the best results as per your knowledge:
Example-I:
var names = new List
names.Add("Gaurav");
var query = names.Select(n=>n.Length*10);
names.Add("Tony");
names.Add("Bikram");
names.Add("Anil");
names.Add("Aditya");
query.Dump("Result from Deferred Query");
Example-II:
var names = new List
names.Add("Gaurav");
var query = names.Select(n=>n.Length*10).ToList();
names.Add("Tony");
names.Add("Bikram");
names.Add("Anil");
names.Add("Aditya");
query.Dump("Result from Deferred Query");
So, what do you thing about results of the above examples. Will the result same in both cases or differ?
Lets take a look into Example-I:
var query = names.Select(n=>n.Length*10);
In above 'query' contains IEnumerable
Now, do you think it is a correct result, if you are thinking yes. Then let me explain:
In above code of line the query did not get fire on the declaration of variable, please note that query returns IEnumerable
So, you can think about a foreach loop, the actual query happened when it iterated all and our 'query' variable will get all beneath to get the result:
names.Add("Tony");
names.Add("Bikram");
names.Add("Anil");
names.Add("Aditya");
This is what we called a 'deferred' execution in Linq. Here, query execution is deferred when it iterates with other elements.
For Example-II:
var query = names.Select(n=>n.Length*10).ToList();
above query executed immediately as we have put a ToList() which enforced query immediate execution. So, result will be only '60'.
There are certain other ways we can invoke immediate query execution viz. Count(), Average(), Max() - for single value results. ToList(), ToDictionary(), ToArray() - for multiple results.
I suggest to read this thread of SO: What are the benefits of a Deferred Execution in LINQ?Closing notes
- In this short article we discussed Deferred and Immediate Query Execution in Linq.
- To use the above code exmaple:
- Download LinqPad
- Type above code example
- Hit F5 and see the results
- To analyse code example in LinqPad check the 'IL' tab from your LinqPad query result window