SIGN UP MEMBER LOGIN:    
ARTICLE

Deferred and immediate executions in LINQ

Posted by Vipul Kelkar Articles | LINQ with C# October 26, 2011
In this article we are going to have a look at how or rather ‘when’ a LINQ query is executed. One execution type of a LINQ query is called deferred execution. This is because the results of the query are evaluated only when required. We will directly dive into code to see what we mean by deferred execution.
Reader Level:

We will create a simple list of strings and query the list through LINQ.

static void Main(string[] args)
{

    List<String> strlist = new List<string>() { "string 1", "string 2", "string 3", "string 4" };
    var q = from strings in strlist
    select strings;
    foreach (string str in q)
    {
        Console.WriteLine(str);
    }
    Console.ReadKey();
}

The output of the above program is

Deferred in liinq

Now refer to the image below

Deferred execution in liinq

How can we check that what we have mentioned in the above image is correct ? Let us add one more string AFTER the LINQ query.

immediate executions in LINQ

So what's the output now ?

Untitled-30.gif

When we added "string 5" after the query, it was still considered in the FOREACH loop. So we can say that with foreach loop, the query was executed only when the values were required.

We can also make the query execute where it was defined by using. ToList() or ToArray() on the LINQ query which will cause the immediate execution of the LINQ query.

immediate execution of the LINQ

And the output is

immediate executions

The query was executed before the foreach loop at the query definition itself because of the use to ToArray() method.

The immediate execution in a LINQ query also takes place if the query is returning single value, like the COUNT for instance

    int count = (from strings in strlist
    select strings).Count();

This query will get executed immediately and assign the count to the variable.

Login to add your contents and source code to this article
share this article :
post comment
 

thx

Posted by Chirag Solanki Nov 15, 2011

Glad to know you liked the article

Posted by Vipul Kelkar Nov 04, 2011

Nice! Tx

Posted by Raanan Nov 04, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor