LINQ: How to check for zero-divider within type?
Is there a way (without using a try catch) to check for zero in ps.sum below so it doesn't throw a divide by zero error?
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,
unitT开发者_Go百科otal = 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
How about:
(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)
}
精彩评论