I have a structure like
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 ? ? ?
Class :
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;
}
I would like to sum value for each column for each id. So in above example, Total
for ID 123 for Principal
would be 150, Interest
200 likewise. I checked this solution over here How to create Total column and Total row using LINQ but not able to get it.
Code:
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 */
})
};
Edit:
To clarify what I am trying to achieve:
- My source record list does not contain "Total" column & "Total" row. I would like to add it manually.
- I Was able to figure out how to Add "Total" column which would contain Sum of values.
Principal
&Interest
will contain values indecimal
format.ID
column is an integer. I would like to Display Total row for eachID
- Values for
Principal
&Interest
are being calculated by aggregating based onID
, After that calculation, onlyTotal
row should get displayed.
Any Suggestions?