User Id :    Password :      New Member   Forgot Password  
 
Difference between Deferred and Immediate execution in Linq
Description In this post, We are going to learn about Deferred vs Immediate execution in Linq. There an interesting variations how Linq operators executes and in this post we are going to learn both Deferred execution and immediate execution.   No. of Views     1516
  Rating     0
Author Jalpesh Vadgama   Posted On     06 Jul 2013
Tags    

Sample Code   Download Code

What is Deferred Execution?
In the Deferred execution query will be executed and evaluated at the time of query variables usage. Let’s take an example to understand Deferred Execution better.

Example:
Following is a code for that.
 

using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var customers = new List<Customer>(
                new[]
                    {
                        new Customer{FirstName = "Jalpesh",LastName = "Vadgama"},
                        new Customer{FirstName = "Vishal",LastName = "Vadgama"},
                        new Customer{FirstName = "Tushar",LastName = "Maru"}
                    }
                );
 
            var newCustomers = customers.Where(c => c.LastName == "Vadgama");
 
            customers.Add(new Customer {FirstName = "Teerth", LastName = "Vadgama"});
 
            foreach (var c in newCustomers)
            {
                Console.WriteLine(c.FirstName);
            }
        }
 
        public class Customer
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
 
    }
}



Here in the above code, I have created a customer class and then created a list of customers. After creating a list of customers I have written a query for selecting customers with last name “vadgama”. After writing that query I have added a new customer “Teerth” with “Vadgama” lastName, I have written a query before adding a new customer it should execute immediate but as where operator support deferred execution it will execute when newCustomer used in the code. So output will like following as expected with deferred execution.

 

 

So the flow of code will be like below.

 

 

 Linq operators that supports deferred execution
 

  •     OrderBy
  •     OrderByDecending
  •     Reverse
  •     Select
  •     SelectMany
  •     Skip
  •     SkipWhile
  •     Take
  •     TakeWhile
  •     Where
  •  

What is immediate execution?
In immediate execution query will be executed immediately. No matter whether collection is changed after it or not.
 

Example:
Let’s take example of immediate execution.
 

using System;
using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var customers = new List<Customer>(
                new[]
                    {
                        new Customer{FirstName = "Jalpesh",LastName = "Vadgama"},
                        new Customer{FirstName = "Vishal",LastName = "Vadgama"},
                        new Customer{FirstName = "Tushar",LastName = "Maru"}
                    }
                );
 
            var newCustomer= customers.LastOrDefault(c => c.LastName == "Vadgama");
 
            customers.Add(new Customer {FirstName = "Teerth", LastName = "Vadgama"});
 
            Console.WriteLine(newCustomer.FirstName);
        }
 
        public class Customer
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
 
    }
}


Here in the above code I have written almost similar code to the deferred code execution just changed the operator LastOrDefault as it’s not supporting deferred execution and it will immediately execute query. Let’s run example to see how it works.
 

 

As expected the “Teerth” customer is the last one as we have added after query.But its displaying “Vishal” as it immediately execute query  So flow of the code will be like below.

 


 Linq operators that supports immediate execution:
 

  •     All
  •     Any
  •     Contains
  •     Count
  •     First
  •     FirstOrDefault
  •     Last
  •     LastOrDefault
  •     Max
  •     Min
  •     Single
  •     SingleOrDefault
  •     Sum
  •     ToArray
  •     ToDictionary
  •     ToList
  •  

That’s it. Hope you like it. Stay tuned for more..
 

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code