title: Calculate total of a column using linq in c#
body: <p>I have a structure like</p>
  
  <pre><code>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        ?            ?           ?
  </code></pre>
  
  <p>Class :</p>
  
  <pre><code>public class APIBillingHistory
  {
      public List&lt;APIBillingHistoryDetails&gt; BillingHistoryDetails;
  }
  
  public class APIBillingHistoryDetails
  {
      public string BillId;
      public string Description;
      public Decimal Principal;
      public Decimal Interest;
      public Decimal Total;
  }
  </code></pre>
  
  <p><strong>I would like to sum value for each column for each id</strong>. So in above example, <code>Total</code> for ID 123 for <code>Principal</code> would be 150, <code>Interest</code> 200 likewise.  I checked this solution over here <a href="https://stackoverflow.com/a/5596761/1169180">How to create Total column and Total row using LINQ</a> but not able to get it.</p>
  
  <p>Code:</p>
  
  <pre><code>responseObj = new APIBillingHistory
  {
      BillingHistoryDetails = billingHistory.BillingHistoryDetails
              .GroupBy(detail =&gt; new { detail.BillId, detail.Description})
              .Select(group =&gt; new APIBillingHistoryDetails
              {
                  BillId = group.Key.BillId,
                  Description = group.Key.Description,
                  Principal = group.Sum(t =&gt; t.Principal),
                  Interest = group.Sum(t =&gt; t.Interest),
                  Total = group.Sum(t =&gt; t.Principal) + group.Sum(t =&gt; t.Interest)
              }).Concat(new APIBillingHistoryDetails
              {
                  Description = "Total",
                  /*  Not sure what to write over here */
              })
  };
  </code></pre>
  
  <p>Edit: </p>
  
  <p>To clarify what I am trying to achieve:</p>
  
  <ul>
  <li>My source record list does not contain "Total" column &amp; "Total" row. I would like to add it manually.</li>
  <li>I Was able to figure out how to Add "Total" column which would contain Sum of values.</li>
  <li><code>Principal</code> &amp; <code>Interest</code> will contain values in <code>decimal</code> format.</li>
  <li><code>ID</code> column is an integer. I would like to Display Total row for each <code>ID</code></li>
  <li>Values for <code>Principal</code> &amp; <code>Interest</code> are being calculated by aggregating based on <code>ID</code>, After that calculation, only <code>Total</code> row should get displayed. </li>
  </ul>
  
  <p>Any Suggestions?</p>