开发者

linq to entity framework: something like isnull

开发者 https://www.devze.com 2023-04-10 10:56 出处:网络
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. Ho

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

0

精彩评论

暂无评论...
验证码 换一张
取 消