c# - 不能将 null 值分配给 decimal 类型的成员,该类型是不可为 null 的值类型

标签 c# .net exception linq-to-sql

枚举以下IQueryable时发生异常:

from record in dataContext.SomeTable
select Convert.ToDecimal(record.nullableDecimalColumn);

错误是 InvalidOperationException:

The null value cannot be assigned to a member with type decimal which is a non-nullable value type

Convert.ToDecimal 的文档说它将 null 转换为 0,因此它看起来应该可以正常工作。

最佳答案

如果你这样写Query

tot_vat_amt = _pd.Where(x => x.VAT == 4m).Sum(x =>x.amount)

替换为

tot_vat_amt = _pd.Where(x => x.VAT == 4m).Sum(x =>(double?)x.amount)

希望对你有帮助

关于c# - 不能将 null 值分配给 decimal 类型的成员,该类型是不可为 null 的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756944/

相关文章:

.net - 哪些版本的 Microsoft.NET 框架适用于 Windows Powershell?

c# - 具有相同签名的成员已经定义了不同的类型约束

phpunit 测试 expectedException 不工作

haskell 。非IO异常处理

c# - 在 ASP.NET MVC 中通过 CancellationToken 中止 AJAX 请求

c# - SQL批量插入,避免重复,无PK

c# - 使用 WPF 在列表框项目上移动 + 单击功能

c# - last_insert_id 多线程

c# - log4net 不记录调试语句

python - 在python中导入类时显示警告