The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

 _investmentRepository.Table.Where(t => t.CustomerId == customerId && t.InvestmentStatus == tempStatus).Sum(t =>t.Amount);

查询结果存在NULL值,在尝试将它转换成System.Int32时出了错,解决方法如下

_investmentRepository.Table.Where(t => t.CustomerId == customerId && t.InvestmentStatus == tempStatus).Sum(t => (decimal?)t.Amount) ?? 0;

原文地址:https://www.cnblogs.com/z-hj/p/5972890.html