I have:
a = b.Sum(..开发者_C百科.) + c.Sum(...)
where b,c
are entities.
The problem is: when at least one of (b.Sum(...)
, c.Sum(...)
) is null then a
will be null. I'd like null be treated as 0. How would I do this?
At least in LINQ2SQL you can cast to int?
manually and then handle the null case
a = ((int?)b.Sum(...) + c.Sum(...)).GetValueOrDefault();
maybe something like this
a = ( b.Sum(...) + c.Sum(...)) ?? 0;
now if the expression is null, a will be 0;
I think you could use null coelesce for this:
a = (b.Sum(..) + c.Sum(...)) ?? 0;
NOTE: unchecked syntax! not sure what your sums are doing!
Have a look at the following MSDN article: http://msdn.microsoft.com/en-us/library/ms173224.aspx
精彩评论