LINQ Deferred Execution

No.of Views713
Bookmarked0 times
Downloads 
Votes0
By  Geming Leader   On  16 Feb 2010 02:02:02
Tag : LINQ , How to
LINQ Deferred Execution
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

 

Introduction

The following example shows how query execution is deferred until the results is enumerated (requested in other words.)

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 0Console.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.

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 10Console.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
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