The inconsistent feeling about Null handling between LinQ and computed property

Answered The inconsistent feeling about Null handling between LinQ and computed property

  • Sunday, July 15, 2012 7:54 PM
     
     

    There is one typical scenario: parent- child summary calculation.

    There are usually existing two level summary calculation.  

    1 the sum between the different child columns. 

     result = this.fee1+ this.fee2;

    2 the sum about the child rows.

    result = this.OrderDetail.Sum(d => d.SubCost);

    The fact is:  

    sum 2 runs like the SQL way, it omits all the null value, and give me the correct number.

    sum 1 runs like the .net way ,  if there exist any null value, there are no result.

    My question: Is there existing any elegant style to handling sum 1 calculation?

    Perhaps, LS team could  add one method like .SafeValue() , which can provide the same function like

    (value.HasValue? value:0)


    • Edited by Bob Zhao Sunday, July 15, 2012 7:55 PM
    •  

All Replies

  • Sunday, July 15, 2012 11:17 PM
    Moderator
     
     Answered Has Code

    There is already something you can use for that. It's called GetValueOrDefault. It only applies to nullable properties.

    It does exactly what you describe.

    So your code would read:

    result = this.fee1.GetValueOrDefault+ this.fee2.GetValueOrDefault;

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

  • Monday, July 16, 2012 8:15 PM
     
     

    Wow, it works fine! Which this candy, it can save me a lot of time to struggle with the type safe issue and focus on the real calculation again!

    Thanks a lot , Yann!