Difference between Deferred execution and Immediate execution

Difference between Deferred execution and Immediate execution

29 Mar 2024
Advanced
8.56K Views
1 min read
Learn via Video Course & by Doing Hands-on Labs

ASP.NET MVC with WebAPI Course

LINQ provides a common query syntax to query any data source. In a LINQ query, you always work with objects. The object might be in-memory of the program or remote object i.e. out memory of the program. Based on object, LINQ query expression is translated and executed.

There are two ways of LINQ query execution as given below:

Deferred Execution

In case of differed execution, a query is not executed at the point of its declaration. It is executed when the Query variable is iterated by using loop for, foreach etc.

DataContext context = new DataContext();
var query = from customer in context.Customers
 where customer.City == "Delhi"
 select customer; // Query does not execute here

 foreach (var Customer in query) // Query executes here
 {
 Console.WriteLine(Customer.Name);
 }

A LINQ query expression often causes deferred execution. Deferred execution provides the facility of query reusability since it always fetches the updated data from the data source which exists at the time of each execution.

Immediate Execution

In case of immediate execution, a query is executed at the point of its declaration. The query which returns a singleton value (single value or a set of values) like Average, Sum, Count, List etc. caused Immediate Execution.

You can force a query to execute immediately of by calling ToList, ToArray methods.

DataContext context = new DataContext();
var query = (from customer in context.Customers
 where customer.City == "Delhi"
 select customer).Count(); // Query execute here

Immediate execution doesn't provide the facility of query re-usability, since it always contains the same data which is fetched at the time of query declaration.

What do you think?

I hope you will enjoy deferred and immediate while programming with LINQ. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this