枚举以下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/