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 results and contains 60 (as very first list element).

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 which is an iterable and needs a yield for next result.
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


Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 149 articles authored by Gaurav Aroraa

Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: