0

有没有办法(不使用 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();
4

2 回答 2

2
PTPcntOfUT = ps.sum != 0 ? Math.Round((g.CurrVal/ps.sum)*100) : valueIfSumIsZero
于 2011-04-27T20:09:47.747 回答
0

怎么样:

(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)
              }
于 2011-04-27T20:10:39.540 回答