LINQ: Deferred Execution

No.of Views1736
Bookmarked0 times
Downloads 
Votes0
By  Geming Leader   On  02 Sep 2010 10:09:58
Tag : LINQ , General
The following example shows how query execution is deferred until the results is enumerated.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

This article is also available in my blog, Just Like a Magic.

The following example shows how query execution is deferred until the results is enumerated.

static void TryLinq()
 {
     int i = 0;
     int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
 
     // fake values for the query only (10 values)
 
     var result = from n in numbers select Increment(ref i);
 
     Console.WriteLine("After query i = {0}", i); // i still 0
 
     Console.WriteLine();
 
     Console.WriteLine("Enumerating results:");
     foreach (var v in result)
     {
         Console.WriteLine("v = {0},ti = {1}", v, i);
         // i is incremented every loop
     }
 
     Console.WriteLine("Press any key to continue . . .");
     Console.ReadKey(true);
 
     // Result:-
 
     // After query i = 0
     //
     // Enumerating results:
     // v = 1,  i = 1
     // v = 2,  i = 2
     // .............
     // v = 9,  i = 9
     // v = 10, i = 10
 
     // What you get?
     // Deferred-Execution / Lazy-Execution
     // - Query doesn't execute until you
     //   begin retrieving it's results.
     // - Every time you try get a value
     //   the query executes on this value only.
 }
 
 static int Increment(ref int i) { return ++i; }

Next is an example shows how you can immediate-execute the query.

static void TryLinq()
{
    int i = 0;
    int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    // fake values for the query only (10 values)

    var result =
        (from n in numbers select Increment(ref i)).ToList();
    // The last call tries to get the value immediately.

    Console.WriteLine("After query i = {0}", i); // i is 10

    Console.WriteLine();

    Console.WriteLine("Enumerating results:");
    foreach (var v in result)
    {
        Console.WriteLine("v = {0},ti = {1}", v, i);
        // i still 10 every loop
    }

    Console.WriteLine("Press any key to continue . . .");
    Console.ReadKey(true);

    // Result:-

    // After query i = 10
    //
    // Enumerating results:
    // v = 1,  i = 10
    // v = 2,  i = 10
    // .............
    // v = 9,  i = 10
    // v = 10, i = 10

    // What you get?
    // Deferred-Execution / Lazy-Execution
    // - Query doesn't execute until you
    //   begin retrieving it's results.
    // - Every time you try get a value the
    //   query executes on this value only.
    // - You can immediate-execute the query by
    //   calling some conversation methods like
    //   ToList or ToQuery.
}

static int Increment(ref int i) { return ++i; }

What you get? Deferred-Execution / Lazy-Execution

  • Query doesn't execute until you begin retrieving it's results.
  • Every time you try get a value the query executes on this value only.
  • You can immediate-execute the query by calling some conversation methods like ToList or ToQuery.
 
Sign Up to vote for this article
 
About Author
 
Geming Leader
Occupation-Software Engineer
Company-Just Like a Magic
Member Type-Expert
Location-Egypt
Joined date-30 Jul 2009
Home Page-http://WithDotNet.net
Blog Page-http://JustLikeAMagic.com
Independent software developer, trainer, and technical writer from Egypt born in 1991
 
 
Other popularSectionarticles
    In this snippet I am going to discuss about how to get list of duplicate items for the collection that we do in sql.
    Published Date : 05/Mar/2011
    Usage of Except Operator in LINQ
    Published Date : 28/May/2010
    Linq contains lots useful operators and i have found more two operators that can be help full in our day to day programming life. Here are explanation.
    Published Date : 21/Jun/2010
    Microsoft .NET framework 4.0 is having many features that make developers life very easy. Its also provides some enhancement to Linq also. I just found a great operator called Zip which merge the sequence of two entities.
    Published Date : 21/Jun/2010
    I got curious about the how well the LINQ implementation will perform and thought of writing this article.
    Published Date : 16/Apr/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top