强制转换为值类型“System.Double”失败的原因

本篇内容主要讲解“强制转换为值类型“System.Double”失败的原因”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“强制转换为值类型“System.Double”失败的原因”吧!

目前成都创新互联公司已为上1000家的企业提供了网站建设、域名、虚拟主机、网站托管、服务器托管、企业网站设计、松原网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

  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.

主要原因是没有适合查询条件的行 去计算,

所以改成以下写法就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);

                }

到此,相信大家对“强制转换为值类型“System.Double”失败的原因”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


分享题目:强制转换为值类型“System.Double”失败的原因
当前地址:http://hxwzsj.com/article/pgcpdo.html

其他资讯

Copyright © 2025 青羊区翔捷宏鑫字牌设计制作工作室(个体工商户) All Rights Reserved 蜀ICP备2025123194号-14
友情链接: 重庆企业网站建设 移动网站建设 温江网站设计 成都网站制作 重庆网站建设 成都定制网站建设 外贸营销网站建设 成都网站建设 品牌网站建设 广安网站设计 成都企业网站制作 重庆企业网站建设 四川成都网站建设 梓潼网站设计 网站设计 网站建设方案 重庆网站制作 成都做网站建设公司 网站建设公司 古蔺网站建设 成都网站建设 高端定制网站设计