This is a migrated thread and some comments may be shown as answers.

query evaluation time

2 Answers 63 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
laai
Top achievements
Rank 1
laai asked on 20 Jan 2011, 01:44 PM

hello,

as i started up with linq, i wrote the following simple query:

 

String[] alphabet = {"alpha", "beta", "gamma", "delta", "epsilon" };

var myQuery = from letter in alphabet 

where letter.Length > 4

select letter;

foreach (var letter in myQuery)

//code to display letter

what i don't understand is the following:

when in debugging mode, after stepping over the statement var myQuery = ... ;, the Locals Windows shows in the Results View corresponding to myQuery, the expected result for the query (the IEnumerable collection alpha, gamma, delta and epsilon).

so the result of this query is computed before entering the foreach loop. then why, inside the execution of the foreach loop, does the variable letter gets first the value "alpha", then the execution goes to the where keyword in the query, to check the filtering predicate and if the predicate is verified it goes back to the foreach, displays the val and then variable letter gets value "beta" and the same execution pattern follows?

thank you

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Jan Blessenohl
Telerik team
answered on 20 Jan 2011, 03:41 PM
Hi laai,
The Locals Windows is displaying the result by touching the property as well. Means the query result is calculated once by the debugger and once in your code, if you close the local window, the code runs like outside the debugger.
You have to be careful with the debugger displaying data, esp. with OpenAccess and lazy loading you execute queries by just looking at the properties.

Best wishes,
Jan Blessenohl
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
0
laai
Top achievements
Rank 1
answered on 20 Jan 2011, 03:59 PM
thanks, jan, this explains another unanswered question
good day

Tags
LINQ (LINQ specific questions)
Asked by
laai
Top achievements
Rank 1
Answers by
Jan Blessenohl
Telerik team
laai
Top achievements
Rank 1
Share this question
or