转换为值类型“Double”失败,因为物化值为空x.UserId==user.UserId&&x.DateCharged>=dateStart&&x.DateChargedx.Quantity*x.Price);错误:转换为值类型“Double”失败,因为具体化值为空。结果类型泛型参数或查询必须使用可空类型。我所看到的:转换为值类型“Int32”失败,因为具体化值为null转换为值类型“Decimal”失败,因为具体化值为null我所做的:doublecafeSales=db.InvoiceLines.Where(x=>x.UserId==user.UserId&&x.DateCharged>=dateStart&&x.DateChargedx.Quantity*x.Price);和:双?cafeSales=db.InvoiceLines.Where(x=>x.UserId==user.UserId&&x.DateCharged>=dateStart&&x.DateChargedx.Quantity*x.Price);这些都不起作用。我知道问题的原因是表中没有我传入的UserId的行。在这种情况下,我希望Sum()只返回0给我。有任何想法吗?最佳解决方案doublecafeSales=db.InvoiceLines.Where(x=>x.UserId==user.UserId&&x.DateCharged>=dateStart&&x.DateCharged(double?)(x.Quantity*x.Price))??0;您可以检查集合是否有正确的结果。双倍的?咖啡馆销售=空;varinvoices=db.InvoiceLines.Where(x=>x.UserId==user.UserId&&x.DateCharged>=dateStart&&x.DateChargedx.Quantity!=null&&x.Price!=null);if(invoices.Any()){cafeSales=invoices.Sum(x=>x.Quantity*x.Price);我知道这有点旧,但万一它对任何人都有帮助。@Matt我认为DefaultIFEmpty()方法应该适用于您,以防您为要应用Sum的列传递默认值。此方法有一些您可能需要检查的重载,如果重载不支持您的要求,我建议使用类型转换。(query).DefaultIfEmpty(0)这应该可以解决问题(如果Quantity或Price不可为空,您可能必须删除其中一个条件):varcafeSales=db.InvoiceLines.Where(x=>x.UserId==user.UserId&&x.DateCharged>=dateStart&&x.DateChargedx.Quantity*x.Price);}varcafeSales=db.InvoiceLines.Where(x=>x.UserId==user.UserId&&x.DateCharged>=dateStart&&x.DateChargedx.Quantity*x.Price);双我;如果(咖啡馆销售==空)?i=0:i=(double)cafeSales.First();上述解决方案对我不起作用。我的问题非常相似。我确定没有返回任何行,但Sum的行为很奇怪。所以我决定在调用lambda表达式之前添加一个检查,我检查lambda返回的行的计数属性。如果它大于零,那么我调用求和表达式。这对我有用。在ctx.EF.Collat??erals中加入simnew{id=ini.cam.id,Type=0}等于new{id=sim.CampaignId==null?newGuid():sim.CampaignId,sim.Type}intotempcollfromsimintempcoll.DefaultIfEmpty()此解决方案有效。实际上,您需要使用三元运算符来检查值并在第二列和第二个表中插入Guid(如果为null)。它会起作用。“Conversiontovaluetype'Double'failedbecausematerializedvalueisnull”将得到解决谢谢正在使用,需要了解更多C#学习教程。希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
