Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Reasons for failure to cast to value type "System.Double"

Shulou Source: shulou.com Published: 2022-06-02 10:08:50 11月15日 Update

This article mainly explains "the reason why casting to value type"System.Double"fails". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "the reason why casting to value type"System.Double"fails"!

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.

The main reason is that there are no rows suitable for query conditions to calculate,

So change it to OK

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); } At this point, I believe that everyone has a deeper understanding of the "reason why the cast to the value type"System.Double"fails". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Reason type compulsion content learning inquiry practicality depth interest writing practicality practicality simplicity of operation method time more friends conditions websites channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple MariaDB Microsoft NVidia