Linq to Sql Sum,无记录 [英] Linq to Sql Sum with no records
本文介绍了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,无记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文