2017-06-26 61 views
-2

我有這樣計算總使用LINQ列在c#

ID Description Principal Interest Total 
123 Paid  100   150  250 
    Balance  50   50   100 
    Total  ?   ?   ? 
124 Paid  100   150  250 
    Balance  50   50   100 
    Total  ?   ?   ? 

類的結構:

public class APIBillingHistory 
{ 
    public List<APIBillingHistoryDetails> BillingHistoryDetails; 
} 

public class APIBillingHistoryDetails 
{ 
    public string BillId; 
    public string Description; 
    public Decimal Principal; 
    public Decimal Interest; 
    public Decimal Total; 
} 

我想爲每列總和值對於每個ID。因此,在上面的例子中,對於,ID 123的Total同樣是150,Interest200。我在這裏檢查了這個解決方案How to create Total column and Total row using LINQ,但無法得到它。

代碼:

responseObj = new APIBillingHistory 
{ 
    BillingHistoryDetails = billingHistory.BillingHistoryDetails 
      .GroupBy(detail => new { detail.BillId, detail.Description}) 
      .Select(group => new APIBillingHistoryDetails 
      { 
       BillId = group.Key.BillId, 
       Description = group.Key.Description, 
       Principal = group.Sum(t => t.Principal), 
       Interest = group.Sum(t => t.Interest), 
       Total = group.Sum(t => t.Principal) + group.Sum(t => t.Interest) 
      }).Concat(new APIBillingHistoryDetails 
      { 
       Description = "Total", 
       /* Not sure what to write over here */ 
      }) 
}; 

編輯:

爲了澄清什麼,我想實現:

  • 我的源記錄列表中不包含 「總計」 列& 「總計」 行。我想手動添加它。
  • 我能弄清楚如何添加將包含值的總和「總計」列。
  • Principal & Interest將包含decimal格式的值。
  • ID列是整數。我想正在通過聚合基於ID計算,即計算後,應該得到只顯示Total行顯示總計行每個ID
  • Principal & Interest

有什麼建議嗎?

+0

對於第二行和第三行,ID列等於什麼?一個'Decimal'怎麼能成爲'?'?你想總結'Total'還是你的意思是'Total'行不在你的源數據中? – NetMage

+0

此外,您的源數據是否已包含每行的「總計」字段,還是必須添加? – NetMage

+0

我想推薦使用名爲LinqPad的工具。這就像使用Sql Server Management Studio,但在C#實體上一樣。允許您可視化LINQ表達式的結果並真正理解發生了什麼。 –

回答

0

由身份

BillId Description Principal Interest 
123 Paid  100   150  
123 Balance  50   50  
124 Paid  100   150  
124 Balance  50   50  

然後,只需組假設當前數據,通過總結分組屬性設置描述爲「總」創建總目標。

var totalsObj = new APIBillingHistory { 
    BillingHistoryDetails = billingHistory.BillingHistoryDetails 
      .GroupBy(detail => detail.BillId) 
      .Select(g => new APIBillingHistoryDetails { 
      BillId = g.Key, 
      Description = "Total", 
      Principal = g.Sum(detail => detail.Principal), 
      Interest = g.Sum(detail => detail.Interest), 
      Total = g.Sum(detail => detail.Principal) + g.Sum(detail => detail.Interest) 
      }).ToList() 
}; 

totalsObj.BillingHistoryDetails本來期望了把

是經過計算,只有總計行應該得到顯示。

BillId Description Principal Interest Total 
123 Total  150   200   350 
124 Total  150   200   350 
1

您不能使用Concat並參考以前的數據。您需要使用SelectMany

BillingHistoryDetails = billingHistory.BillingHistoryDetails 
    .GroupBy(detail => detail.BillId) 
    .SelectMany(bg => bg.Concat(new[] { new APIBillingHistoryDetails { 
     BillId = bg.First().BillId, 
     Description = "Total", 
     Principal = bg.Sum(b => b.Principal), 
     Interest = bg.Sum(b => b.Interest), 
     Total = bg.Sum(b => b.Total) 
    } })); 
+0

我不能避免'Select',因爲我正在通過'ID'對'Principal'和'Interest'進行聚合。在做這個聚合後,我想計算每列的總數。 – Shaggy

+0

這在你的問題中並不明顯。 – NetMage

+0

已編輯的問題。 – Shaggy