2011-04-27 37 views
0

有沒有一種方法(不使用try catch)檢查下面的ps.sum中的零,所以它不會拋出除零錯誤?LINQ:如何檢查類型內的零分頻器?

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(); 

回答

2
PTPcntOfUT = ps.sum != 0 ? Math.Round((g.CurrVal/ps.sum)*100) : valueIfSumIsZero 
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) 
       }