Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Deferred VS Immediate Query Execution in LINQ

Viewed:  2105 
Posted On:  04/06/2015 21:31:52 
What is Deferred Execution and Immediate Query Execution in LINQ? What is the difference between Deferred Execution and Immediate Query Execution? 

In this article I am going to explain difference between Deferred & Immediate Query Execution in LINQ.

In Deferred Execution query is not executed at the time of declaration. When the query object iterated over a loop then its executed.

In Immediate Execution query is got executed when it declared.

Deferred Execution:

Now we will see both by using an example. For this I created a Visual Studio Solution. Here In my solution I added a new Class Employee Like below:


Image 1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DeferredVsImmediate_Query
    public class Employee
        public int Emp_ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Country { get; set; } 

Add a New Web Page Default.aspx and Add Below Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DeferredVsImmediate_Query
    public partial class Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            var Emp_List = new List<Employee>(
                new Employee[]
                    new Employee{Emp_ID=1, Name="Shambhu Sharma", Email="", Country="India"},
                    new Employee{Emp_ID=2, Name="Manu Khanna", Email="", Country="India"},
                    new Employee{Emp_ID=3, Name="Abhishek Nigam", Email="", Country="USA"},
                    new Employee{Emp_ID=4, Name="Yogesh Gupta", Email="", Country="USA"},
                    new Employee{Emp_ID=5, Name="Shweta Kashyap", Email="", Country="India"},
                    new Employee{Emp_ID=6, Name="Shraddha Gaur", Email="", Country="India"},
                    new Employee{Emp_ID=7, Name="Akhilesh Atwal", Email="", Country="India"},
                    new Employee{Emp_ID=6, Name="Mayank Dhulekar", Email="", Country="USA"},
                    new Employee{Emp_ID=7, Name="Saurabh Mehrotra", Email="", Country="USA"},
                    new Employee{Emp_ID=7, Name="Mehak Jain", Email="", Country="India"},
            var Result = from a in Emp_List
                         where a.Country.Equals("India")
                         select new { a.Name };
            foreach (var EMP in Result)
                Response.Write(EMP.Name + "</br>");

Now Run Your Application:


Image 2.

Now we will see when my query executed:


Image 3.

Now Add a new record after your query to see deferred execution:


Image 4.

Now run your app:


Image 5.

Immediate Execution:  We can force our query to execute immediately like below:


Image 6.

Now run your application:


Image 7.

Now See Both:


Image 8.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us