Deferred Execution and Immediate Execution in LINQ

Deferred Execution executes the query only when Loop starts. What I mean here is that, we iterate through the query variable to get the result.
  

clip_image001

  

Here the result variable does not contain the actual result. To get the result, we may iterate through the result (query) variable in loop as many time as we want. We do deferred execution mainly when multiple values is being returned from the query.

  

Example

  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
 class Program
 {
 static void Main(string[] args)
 {
 DataClasses1DataContext context = new DataClasses1DataContext();
 var result = from r in context.Persons select r;
 foreach (var a in result)
 {
 Console.WriteLine(a.FirstName + "" + a.LastName);
 }
 Console.ReadKey(true);

 }
 }
}

  

Output
clip_image003

  

Immediate Execution happens when LINQ query returns a single value.

  

clip_image005

  

Above query is returning a single value so it can be executed immediately.

  

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
 class Program
 {
 static void Main(string[] args)
 {
 DataClasses1DataContext context = new DataClasses1DataContext();
 var numberOfRecords = (from r in context.Persons select r).Count();
 Console.WriteLine(numberOfRecords);
 Console.ReadKey(true);

}
 }
}

  

Output
clip_image007

  

If we want to make a query returning multiple values or sequence of values as immediate then we need to explicitly convert the result in ToList().

  

clip_image009

  

So above query is returning multiple values and would be executed immediately.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication3
{
 class Program
 {
 static void Main(string[] args)
 {
 DataClasses1DataContext context = new DataClasses1DataContext();
 var result1 = (from r in context.Persons select r).ToList();
 foreach (var a in result1)
 {
 Console.WriteLine(a.FirstName + "" + a.LastName);
 }
 Console.ReadKey(true);
 }
 }
}
Output
clip_image011

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Posted in LINQ
One comment on “Deferred Execution and Immediate Execution in LINQ

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,132 other followers

%d bloggers like this: