Why don't the values from my linq queries appear immediately?

后端 未结 3 986
一生所求
一生所求 2020-12-10 18:42

I have the following block of linq queries to calculate some values for a report.

var items = (from trans in calclabordb.Sales_Transactions                
          


        
相关标签:
3条回答
  • 2020-12-10 18:50

    I think you're a "victim" of deferred execution in LINQ. Check out this blog post which explains the concept:

    http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx

    0 讨论(0)
  • 2020-12-10 18:55

    Yep, it's called deferred execution and it's one of the things that can be really powerful, but also trip you up if you aren't expecting it...

    0 讨论(0)
  • 2020-12-10 19:02

    The answers that say that this is a deferred execution problem are wrong. It is an operator precedence problem.

    Get rid of all that completely irrelevant and impossible-to-read code in there. It is all red herring. The relevant repro is:

    decimal? d1 = 11.31m;
    decimal? d2 = 11.28m;
    decimal test1 = d1 ?? 0m;
    decimal test2 = d2 ?? 0m;
    decimal test3 = test1 + test2;
    decimal test4 = d1 ?? 0m + d2 ?? 0m;
    

    What is the meaning of the final line? Does it mean the same thing as the line before it?

    No, it does not. The addition operator is higher precedence than the null coalescing operator, so this is

    decimal test4 = d1 ?? (0m + d2) ?? 0m;
    

    The code you wrote means "produce the value of d1 if d1 is not null. If d1 is null and 0m + d2 is not null then produce the value of 0m + d2. If 0m + d2 is null then produce the value 0m."

    (You might not have known that the ?? operator has this pleasant chaining property. In general, a ?? b ?? c ?? d ?? e gives you the first non-null value of a, b, c or d, and e if they are otherwise all null. You can make the chain as long as you like. It's quite an elegant little operator.)

    Since d1 is not null, we produce its value and test4 is assigned the value of d1.

    You probably meant to say:

    decimal test4 = (d1 ?? 0m) + (d2 ?? 0m);
    

    If you mean "d1 or d2 could be null, and if either is, then treat the null one as zero". So 12 + 2 is 14, 12 + null is 12, null + null is 0

    If you mean "either d1 and d2 could be null, and if either is null then I want zero", that's

      decimal test4 = (d1 + d2) ?? 0m;
    

    So 12 + 2 is 14, 12 + null is 0, null + null is 0

    I note that if you had formatted your code so that the relevant text was on the screen, you probably wouldn't have gotten five or so incorrect answers posted first. Try to format your code so that all of it is on the screen; you'll get better answers if you do.

    0 讨论(0)
提交回复
热议问题