Please enable Javascript to correctly display the contents on Dot Net Tricks!

Difference between Deferred execution and Immediate execution

  Author : Shailendra Chauhan
Updated On : 16 Jul 2014
Total Views : 53,798   
 

LINQ provides a common query syntax to query any data source. In a LINQ query, you always work with objects. The object may in-process object or out-process object. Based on objects, 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 like as for, foreach.

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 (a 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.


YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

14 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+