foreach (var item in entityList) { ChannelReportDto channelReportDto = new ChannelReportDto(); channelReportDto.Id = item.Id; channelReportDto.YueziCenterId = item.YueziCenterId; channelReportDto.YueziCenterName = item.YueziCenterName; channelReportDto.ChannelName = item.ChannelName; channelReportDto.SumProfitAmount = OrderServiceProjectresult.Where(w => w.ChannelId == item.Id && w.IsDeleted == false).Select(s => s.SumProfitAmount).Sum(); channelReportDto.SettleProfitAmount = OrderServiceProjectresult.Where(w => w.ChannelId == item.Id && w.IsSettlement == true).Select(s => s.SumProfitAmount).Sum(); channelReportDto.NoSettleProfitAmount = OrderServiceProjectresult.Where(w => w.ChannelId == item.Id && w.IsSettlement == false).Select(s => s.SumProfitAmount).Sum(); list.Add(channelReportDto); }
这样写的时候 就报了这个错 The cast to value type 'System.Double' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.code
主要缘由是没有适合查询条件的行 去计算,it
因此改为如下写法就OKast
foreach (var item in entityList) { ChannelReportDto channelReportDto = new ChannelReportDto(); channelReportDto.Id = item.Id; channelReportDto.YueziCenterId = item.YueziCenterId; channelReportDto.YueziCenterName = item.YueziCenterName; channelReportDto.ChannelName = item.ChannelName; channelReportDto.SumProfitAmount = OrderServiceProjectresult.Where(w => w.ChannelId == item.Id && w.IsDeleted == false).Sum(s =>(double?)s.SumProfitAmount)??0.00; channelReportDto.SettleProfitAmount = OrderServiceProjectresult.Where(w => w.ChannelId == item.Id && w.IsSettlement == true).Sum(s => (double?)s.SumProfitAmount) ?? 0.00; channelReportDto.NoSettleProfitAmount = OrderServiceProjectresult.Where(w => w.ChannelId == item.Id && w.IsSettlement == false).Sum(s => (double?)s.SumProfitAmount) ?? 0.00; list.Add(channelReportDto); }