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); }