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

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.


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


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


Comments

  1. Ryan

    • 2018/6/22

    6.1m Followers, 63 Following, 880 Posts - See Instagram photos and videos from Why Don't We (@whydontwemusic)

  2. Perez

    • 2020/11/3

    Why Don’t We ignite this chapter with the single “Fallin’.” Opening with an iconic drumline, the track steamrolls out of the gate towards a magnetic and massive refrain punctuated by fiery falsetto and a chant, “Maybe, we’ll just keep falling.” “Lyrically, I was thinking of a girl,” reveals Daniel.

  3. Andre

    • 2016/9/16

    https://youtu.be/vhYEmZYS9fE This Thursday on RELEASED, go behind the scenes with Why Don't We as they talk about how well they know each other and their new 

  4. Foster

    • 2015/3/23

    View Details. Why Don't We. The Good Times and The Bad Ones (Exclusive Clear Vinyl) $24.98. View Details. Why Don't We. The Good Times and The Bad Ones (Standard Black Vinyl) $19.98. View Details.

  5. Darren

    • 2021/1/4

    Why Don't We's new album "The Good Times and the Bad Ones" featuring "Fallin' (Adrenaline)", "Lotus Inn", "Slow Down" & "Grey" available everywhere now.

  6. Jefferson

    • 2017/9/26

    'Talk' from our debut album out now: http://whydntwe.co/talkLIMITED EDITION 8 LETTERS PRE-ORDER BUNDLEShttp://whydntwe.co/wdwmerchytFOLLOW US https://twitter

  7. Lukas

    • 2020/9/11

    The latest Tweets from Why Don't We (@whydontwemusic). we're a band. text us +1 323-348-1663.

  8. Rossi

    • 2017/6/30

    Why Don't We, shortened to WDW, is an American boy band consisting of Jack Avery, Corbyn Besson, Zach Herron, Jonah Marais, and Daniel Seavey. Formed in 2016, the group has released two studio albums and six extended plays, and has been nominated for an MTV Video Music Award

  9. Stewart

    • 2019/8/25

    Why Don't We, shortened to WDW, is an American boy band consisting of Jack Avery, Corbyn Besson, Zach Herron, Jonah Marais, and Daniel Seavey.

  10. Marcel

    • 2018/9/12

    Slow Down out now! Listen here: https://WhyDontWe.lnk.to/SlowDownIDNew album "The Good Times And The Bad Ones" out now: https://whydontwe.lnk.to/TGTATBOSHOP

  11. Augustine

    • 2019/8/26

    Listen to Why Don't We on Spotify. Artist · 7.5M monthly listeners.

  12. Kenzo

    • 2021/5/23

    The official audio of "Choose" by Why Don't We from the album '8 Letters'. Why Don't We - '8 Letters'Debut Album Out Now: https://Atlantic.lnk.to/8LettersIDF

  13. Kristian

    • 2015/10/12

    Why Don't We. 911784 likes · 47669 talking about this. we're a band.

  14. Liam

    • 2017/3/6

    When five guys decided to form a band, they never imagined that they would go from playing music online to playing tours across the world so quickly. Why Don't 

Comments are closed.

Recent Posts