Deferred vs Immediate Query Execution in LINQ

Posted by: Suprotim Agarwal , on 8/12/2011, in Category LINQ
Views: 171932
Abstract: In LINQ, queries have two different behaviors of execution: immediate and deferred. In this article, we will take a quick overview of how Deferred query execution and Immediate Query Execution works in LINQ

In LINQ, queries have two different behaviors of execution: immediate and deferred.

In this article, we will take a quick overview of how Deferred query execution and Immediate Query Execution works in LINQ, and the difference between the two.

Note: If you do LINQ programming, do check my article 50 LINQ Examples, Tips and How To's

Deferred vs Immediate Query Execution

Deferred Query Execution

To understand Deferred Query Execution, let’s take the following example which declares some Employees and then queries all employees with Age > 28:


LINQ Deferred Execution

OUTPUT: Jack, Rahul

Looking at the query shown above, it appears that the query is executed at the point where the arrow is pointing towards.

However that’s not true.

The query is actually executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution.

Now how do we prove that the query was not executed when the query variable was created?

It’s simple. Just create another Employee instance after the query variable is created

LINQ Deferred execution

Notice we are creating a new Employee instance after the query variable is created.

Now had the query been executed when the query variable is created, the results would be the same as the one we got earlier, i.e. only two employees would meet the criteria of Age > 28.

However the output is not the same

OUTPUT: Jack, Rahul, Bill.

What just happened is that the execution of the query was deferred until the query variable was iterated over in a foreach loop.

Deferred execution is important as it gives you the flexibility of constructing a query in several steps by separating query construction from query execution.

This allows you to execute a query as frequently as you want to, like fetching the latest information from a database that is being updated frequently by other applications. You will always get the latest information from the database in this case.

Immediate Query Execution

You can also force a query to execute immediately, which is useful for caching query results.

Let us say we want to display a count of the number of employees that match a criteria.

LINQ Immediate Execution

In the query shown above, in order to count the elements that match the condition, the query must be executed, and this is done automatically when Count( ) is called.

So adding a new employee instance after the query variable declaration does not have any effect here, as the query is already executed. The output will be 2, instead of 3.

The basic difference between a Deferred execution vs Immediate execution is that Deferred execution of queries produce a sequence of values, whereas Immediate execution of queries return a singleton value and is executed immediately. Examples are using Count(), Average(), Max() etc.

Note: To force immediate execution of a query that does not produce a singleton value, you can call the ToList(), ToDictionary() or the ToArray() method on a query or query variable. These are called conversion operators which allow you to make a copy/snapshot of the result and access is as many times you want, without the need to re-execute the query.

Hopefully novice developers will now know the basic difference between Deferred and Immediate Execution of queries, and when to use what.

I hope you liked the article and I thank you for viewing it!

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Fifteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Mahesh Panhale on Friday, August 19, 2011 11:17 AM
Very nice explanation :)
Comment posted by Suprotim Agarwal on Monday, August 22, 2011 1:14 PM
Mahesh: Glad you liked the article!
Comment posted by Laughing My Ass Off on Wednesday, February 29, 2012 2:19 PM
You have to be the lamest douche on the planet. Hiding your worthless code in a picture? Don't you know a png can be converted to text easier than any other graphic format? Douche Bag
Comment posted by Vijay Kumar Sharma on Monday, April 30, 2012 3:32 AM
Explained very easy n concise manner..great :)
Comment posted by Deepak Kumar on Wednesday, June 27, 2012 2:09 AM
Nice One !!!! Insight Execution in Linq
Comment posted by sridhar on Sunday, July 1, 2012 2:42 AM
good one.
Comment posted by Not Applicable on Thursday, July 26, 2012 8:25 AM
@Laughing My Ass Off - Don't you know that getting up from your worthless PC and going out and having a life is much more worthwhile than picking faults with other people? Douche Bag
Comment posted by Venkat on Saturday, October 27, 2012 12:28 AM
Very.... Nice... Wow.. really i glad to know this THANKS MUCH Good work
Comment posted by Ankur Jasoria on Monday, October 29, 2012 2:58 AM
Remarkable Explanation !!!!!!
Comment posted by Jitendra kumar on Thursday, November 1, 2012 1:55 AM
This is very nice example..!!!!
Comment posted by Priyansh Rastogi on Friday, November 9, 2012 4:50 AM
very nice Article .................
Comment posted by Prabhat on Tuesday, January 15, 2013 5:49 AM
Nice article. Very good explanation.
Comment posted by Gowtham on Friday, January 25, 2013 5:46 AM
I like your explanation. Its very simple and clear.
Comment posted by Sher Bahadur on Wednesday, March 13, 2013 3:06 AM
Very nice....
Comment posted by Arunagiri on Wednesday, May 22, 2013 7:35 AM
Thank you so much for your nice explanation.
Comment posted by vas on Thursday, June 13, 2013 8:18 PM
This very nice explanation which is simple and clear....
Comment posted by Santosh on Saturday, June 22, 2013 10:03 AM
Good explanation
Comment posted by Arun Antony on Tuesday, July 23, 2013 5:00 AM
Nice article. Really good explanation.
Comment posted by Gowtham on Tuesday, July 23, 2013 11:46 PM
Nice explanation, it's very clear
Comment posted by Ateet Agarwal on Sunday, August 4, 2013 12:39 AM
Thanks for such a nice article.
Comment posted by DineshEzeikel on Thursday, August 8, 2013 4:19 AM
Very nice article, thanks a lot.
Comment posted by Himanshu Pareek on Monday, January 13, 2014 1:16 AM
A very nicely explained. Thanks Buddy.
Comment posted by sanjay Gupta on Monday, February 3, 2014 3:20 AM
Nice Article
Comment posted by jo on Tuesday, February 4, 2014 11:53 PM
Comment posted by Anji on Wednesday, March 12, 2014 5:34 AM
superb explanation on the topic....keep posting such good articles
Comment posted by Suyog on Thursday, April 3, 2014 1:39 AM
Nice One .. Good
Comment posted by JACK on Monday, April 7, 2014 2:08 AM
Comment posted by Seema on Wednesday, April 23, 2014 4:59 AM
Well explained with simple example. Superb!
Comment posted by Seema on Wednesday, April 23, 2014 5:01 AM
Well explained with simple example. Superb!
Comment posted by Andrei on Friday, April 25, 2014 1:10 AM
nice example! thanks!
Comment posted by Pravinkumar on Saturday, September 13, 2014 7:24 AM
Very clear explanation
Comment posted by Gunny on Friday, October 24, 2014 8:55 AM
the way u explain in this article was brillant it is very easy to understand even the beginners in linq. keep posting  this is the way of making the article complete.  
Comment posted by Babu on Sunday, November 9, 2014 7:49 AM
Good Article and explanation.

Thank You.
Comment posted by KARTHIK on Friday, November 28, 2014 4:54 AM
Thanks a lot
Comment posted by Mahesh on Wednesday, December 17, 2014 10:49 PM
Nice one! Better add an example showing .ToList() as well as readers can correlate with the example given for deffered execution.
Comment posted by Ragav on Saturday, March 14, 2015 2:53 AM
Thanks for the nice and clear explanation of Linq execution approaches.