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

Deferred VS Immediate Query Execution in LINQ

Article:
Viewed:  1007 
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:

1.png

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="shambhu@shambhu.com", Country="India"},
                    new Employee{Emp_ID=2, Name="Manu Khanna", Email="manu@manu.com", Country="India"},
                    new Employee{Emp_ID=3, Name="Abhishek Nigam", Email="abhishek@abhishek.com", Country="USA"},
                    new Employee{Emp_ID=4, Name="Yogesh Gupta", Email="yogesh@yogesh.com", Country="USA"},
                    new Employee{Emp_ID=5, Name="Shweta Kashyap", Email="Shweta@Shweta.com", Country="India"},
                    new Employee{Emp_ID=6, Name="Shraddha Gaur", Email="Shraddha@Shraddha.com", Country="India"},
                    new Employee{Emp_ID=7, Name="Akhilesh Atwal", Email="Akhilesh@Akhilesh.com", Country="India"},
                    new Employee{Emp_ID=6, Name="Mayank Dhulekar", Email="Mayank@Mayank.com", Country="USA"},
                    new Employee{Emp_ID=7, Name="Saurabh Mehrotra", Email="Saurabh@Saurabh.com", Country="USA"},
                    new Employee{Emp_ID=7, Name="Mehak Jain", Email="Mehak@Mehak.com", 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:

2.png

Image 2.

Now we will see when my query executed:

3.png

Image 3.

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

4.png

Image 4.

Now run your app:

5.png

Image 5.


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

6.png

Image 6.

Now run your application:

7.png

Image 7.

Now See Both:

8.png

Image 8.

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