2010-08-04 16 views
2

我正在LINQ中寫入一個lambda,以便從結果列表中獲取pivoted數據。爲了獲取pivoting列,我正在設置一個where condion來獲取值。這裏的問題是獲取默認值if在哪裏condtion失敗。我不想要列,如果where條件失敗。請幫助我。使用lambda表達式在LINQ中旋轉

var query = dataList 
    .GroupBy(c => c.IpAddress) 
    .Select(g => new 
    { 
     IPAddress = g.Key, 
     AssetType = g.ElementAtOrDefault(0).AssetTypeName, 
     AssetName = g.ElementAtOrDefault(0).AssetName, 
     //if where condition fails i dont need this column. 
     //also am giving c.DsName == "Active Calls" ,how to achieve tis dynamically 
     **ActiveCalls = g.Where(c => c.DsName == "Active Calls").Sum(c => c.CurrentValue),** 
     **HoldCalls = g.Where(c => c.DsName == "Hold Calls").Sum(c => c.CurrentValue),** 
     **CPU = g.Where(c => c.DsName == "CPU").Sum(c => c.CurrentValue),** 
    }); 
+0

有沒有必要添加一個答案來編輯您的問題... – jeroenh 2010-08-04 11:23:48

回答

1

爲什麼不只是創建一個值來保存總和,然後在另一列中指定它的類型。這樣你就不必處理空列。 (假設一次只有一種類型是有效的)。

Value = g.Sum(c => c.CurrentValue), // Value as specified by the the DsName property. 
DsName = c.DsName