开发者

LINQ: How to check for zero-divider within type?

开发者 https://www.devze.com 2023-03-01 07:18 出处:网络
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?

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)
              }
0

精彩评论

暂无评论...
验证码 换一张
取 消