是否有一种方法(不使用try catch)来检查下面ps.sum中的零,因此它不会抛出除零错误?
var procVsTotPkg = unitGroup.Join(procSums, g => new
{
g.DeptID,
g.UnitLoc,
g.Proc
},
ps => new
{
DeptID = ps.key.deptID,
UnitLoc = ps.key.unitLoc,
Proc = ps.key.procName
}, (g, ps) => new procVSUnitTotalJoin
{
dept = g.DeptID,
uloc = g.UnitLoc,
procName = g.Proc,
unitName = g.Unit,
unitTotal = g.CurrVal,
procTotal = ps.sum,
PTPcntOfUT = Math.Round((g.CurrVal / ps.sum) * 100, 2)
}).Single();
PTPcntOfUT = ps.sum != 0 ? Math.Round((g.CurrVal/ps.sum)*100) : valueIfSumIsZero
如何:
(g, ps) => new procVSUnitTotalJoin
{
dept = g.DeptID,
uloc = g.UnitLoc,
procName = g.Proc,
unitName = g.Unit,
unitTotal = g.CurrVal,
procTotal = ps.sum,
PTPcntOfUT = Math.Round((g.CurrVal / (ps.sum == 0 ? 1:ps.sum)) * 100, 2)
}