Re: Deferred execution versus immediate execution--what's the difference?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sep 16, 11:22 am, raylopez99 <raylope...@xxxxxxxxx> wrote:
I'm reading a book that talks about how some operations, like LINQ
queries, are "deferred execution" while other queries are "immediate
execution".

What is the difference?

RL

Consider this code:

static void Main(string[] args) {

//An array of integers
int[] numbers = { 1, 2, 3, 4};

//query the array for all even numbers
var query = from n in numbers
select Square(n);

//iterate through the results
foreach (int i in query) {
Console.WriteLine(i.ToString());
}


Console.ReadLine();
}

public static double Square(double n) {
Console.WriteLine("Computing Square(" + n + ")...");
return Math.Pow(n, 2);
}

When you run it, you see this output:

Computing Square(1)...
1
Computing Square(2)...
4
Computing Square(3)...
9
Computing Square(4)...
16

Notice that the line that says "Computing Square" for each number is
not executed until the foreach is executed. In other words the Square
method is not called until you iterate the query. But if you change
the foreach to look like this (note the addition of ToList():

//iterate through the results
foreach (int i in query.ToList()) {
Console.WriteLine(i.ToString());
}

The result is this:

Computing Square(1)...
Computing Square(2)...
Computing Square(3)...
Computing Square(4)...
1
4
9
16

All the squares are calculated before they are displayed. The first
example is deferred execution of the query. The second example
(ToList) is immediate execution.

Hope this helps a little.

Chris
.



Relevant Pages

  • Re: Finally which ORM tool?
    ... Also, if you pass a variable to the query, the value the ... you have a linq query and by changing the variable's value, ... q is affected if I change foo AFTER this query and BEFORE execution. ... And it is a declaration, but one which captures the variables. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finally which ORM tool?
    ... the session' method. ... able to execute the query by itself. ... has at EXECUTION time is used, ... That SHOULDN'T be important, simply because q LOOKS like a declaration, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Execution plan
    ... I have 191276 rows in table transportOrder, ... index on transportOrder for your query. ... But as you see from query plan, optimizer didn't use this index at ... Sometimes you have query which executes slow(no matter which execution ...
    (microsoft.public.sqlserver.programming)
  • Re: Finally which ORM tool?
    ... the session' method. ... they use the same mechanism as Linq to Sql does: ... Also, if you pass a variable to the query, the value the variable ... q is affected if I change foo AFTER this query and BEFORE execution. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: One Insert statement, no looping, creates 2 records?
    ... Is there any chance a distributed query is involved? ... you can see this when the first execution generates the ... >REALLY odd, tho, is that the debug display only shows once, even tho it's ... and the debug display ...
    (microsoft.public.sqlserver.programming)