強制轉換爲值類型「System.Double」失敗,由於物化值爲空。結果類型的泛型參數或查詢必須使用可爲空的類型

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

                }
相關文章
相關標籤/搜索