query evaluation time

3 posts, 1 answers
  1. laai
    laai avatar
    2 posts
    Member since:
    Jan 2011

    Posted 20 Jan 2011 Link to this post

    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. Answer
    Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 20 Jan 2011 Link to this post

    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.
  3. DevCraft banner
  4. laai
    laai avatar
    2 posts
    Member since:
    Jan 2011

    Posted 20 Jan 2011 Link to this post

    thanks, jan, this explains another unanswered question
    good day

Back to Top