本文介绍了Linq to Sql Sum 没有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个收集整个月累计总数的方法.问题是在给定月份中某些项目可能不收费,因此不会返回任何行.
I am creating a method that collects accumulated totals throughout the month. The problem is that there may not be charges for some of the items in a given month so no rows would be returned.
我可以看到如果没有数据会出错:
I can see how this would error with no data:
double fuelCost = (double)(from a in db.EquipmentFuelLogs
where a.wdEquipmentMainGeneralOID == vehicleKey &&
(monthBeginDate < a.Date1 && a.Date1 < monthEndDate)
select a.TotalCost).Sum();
检测当月没有燃料交易并将燃料成本设置为 0 的最佳方法是什么?只是尝试捕捉?这篇文章谈到了这个问题,但是没有办法.
What is the best approach to detect no fuel transactions for that month and set the fuel cost to 0? Just do a try catch? This article spoke of the problem but no solution.
推荐答案
我添加了一个 catch 来捕获错误.我从来没有找到不同的解决方案.
I added a catch to catch the error. I never found a different solution.
这篇关于Linq to Sql Sum 没有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!