如何比较Linq中的十进制DBNull?

时间:2016-10-13 03:40:30

标签: c# linq linq-to-sql

someDS.someDT.Where(x => x.Amount is not DBNull)
             .Sum(x => x.Amount);

根据链接How to compare DBNull value,上述代码应该没问题,还是有其他办法来处理上述情况?

2 个答案:

答案 0 :(得分:0)

如果您的Amount是可空的decimal,那么您可以将null可空decimal类型的值视为

someDS.someDT.Where(x => x.Amount.HasValue == true)
             .Sum(x => x.Amount);

答案 1 :(得分:0)

  someDS.someDT.Where(x => x.Amount != DBNull.Value)
         .Sum(x => x.Amount);