title: How to add Sub total row and grand total row using linq
body: <p>I want to add the sub total row for each <strong>BusinessTypeCode</strong> and grand total for all <strong>BusinessTypeCode</strong>. How can I add these two rows in my linq and put the below of each businesstypecode.</p>
  
  <p><strong>MY CURRENT CODE</strong></p>
  
  <pre><code>var query = (from _transaction in _entities.Transactions
               join _cd in _entities.Organisations on _transaction.Refno equals _cd.Refno
               join _b in _entities.BusinessType on _transaction.BusinessTypeCode equals _b.BusinessTypeCode
               group new
               {
                   _trans = _transaction,
                   cd = _cd,
               }
               by new { _transaction.BusinessTypeCode,_transaction.Refno, _cd.BusinessName, _b.Description } into _group
               orderby _group.Key.BusinessTypeCode
               select new
               {
                   BusinessTypeCode = _group.Key.BusinessTypeCode,
                   BusType = _group.Key.BusinessTypeCode + " - " +_group.Key.Description,
                   BusName = _group.Key.BusinessName,
                   BusL = _group.Sum(x=&gt;x._trans.BusL),
                   BusInterrest = _group.Sum(x =&gt; x._trans.BusInterrest),
                   BusAdmin = _group.Sum(x =&gt; x._trans.BusAdmin),
                   BusPenalty = _group.Sum(x =&gt; x._trans.BusPenalty),
                   TotalBusCollected =_group.Sum(x=&gt;x._trans.TotalBusCollected)
               });
  DataTable dt=new DataTable();
  DataSet ds = new DataSet();
  ds.Tables.Add(query.CopyToDataTable());
  ds.Tables[0].TableName = "Table1";
  var subtotal = query.GroupBy(x=&gt;x.BusinessTypeCode ).Select(s=&gt;new
  {
      BusinessTypeCode =s.Key,
      BusLSub = s.Sum(x=&gt;x.BusL),
      BusInterrestSub = s.Sum(x=&gt;x.BusInterrest),
      BusAdminSub = s.Sum(x=&gt;x.BusAdmin),
      BusPenaltySub = s.Sum(x=&gt;x.BusPenalty),
      TotalBusCollectedSub = s.Sum(x=&gt;x.TotalBusCollected),
  });
  foreach (var a in subtotal)
  {
      dt = ds.Tables[0];
      dt.NewRow();
      dt.Rows.Add(a.BusLSub, a.BusInterrestSub, a.BusAdminSub, a.BusPenaltySub, a.TotalBusCollectedSub );
  }
  return ds;
  </code></pre>
  
  <p><strong>Current Output</strong></p>
  
  <pre><code>BusType        |BusName   | BusL  |BusInterest|BusAdmin| BusPenalty|TotalBusCollected
  1 - ACCOUNTING |HIGHVELD  |-23.91 | 0         |-22.84  | 0         |-46.75
  1 - ACCOUNTING |BHP       |-50.81 |-79.21     |-76     |-20.02     |-226.04
  2 - FOOD       |SAB       |-14.18 |-435.97    |-2.57   |-67.55     |-520.27
  2 - FOOD       |DISTIL    |-43.05 |0          |-66,59  |0          |-109.64
  3 - MINING     |ANGLOGOLD |-4.43  |0          |-72     |0          |-76.43
     -74.72      |-79.21    |-98.84 |-20.02     |-272.79
     -57.23      |-435.97   |-69.16 |-67.55     |-629.91
     -4.43       |0         |-72    |0          |-76.43
  </code></pre>
  
  <p>How can I push it into where <code>BusinessTypeCode</code> =BusinessTypeCode  ?</p>
  
  <p><strong>OUTPUT SUPPOSE TO BE LIKE</strong></p>
  
  <pre><code>   BusType     |BusName   | BusL  |BusInterest|BusAdmin| BusPenalty|TotalBusCollected
  1 - ACCOUNTING |HIGHVELD  |-23.91 | 0         |-22.84  | 0         |-46.75
  1 - ACCOUNTING |BHP       |-50.81 |-79.21     |-76     |-20.02     |-226.04
  --------------------------+-------+-----------+--------+-----------+-----------------
  Sub Total                 |-74.72 |-79.21     |-98.84  |-20.02     |-272.79
  --------------------------+-------+-----------+--------+-----------+-----------------
   2 - FOOD      |SAB       |-14.18 |-435.97    |-2.57   |-67.55     |-520.27
   2 - FOOD      |DISTIL    |-43.05 |0          |-66,59  |0          |-109.64
  --------------------------+-------+-----------+--------+-----------+-----------------
  Sub Total                 |-57.23 |-435.97    |-69.16  |-67.55     |-629.91
  --------------------------+-------+-----------+--------+-----------+-----------------
  3 - MINING     |ANGLOGOLD |-4.43  |0          |-72     |0          |-76.43
  --------------------------+-------+-----------+--------+-----------+-----------------
  Sub Total                 |-4.43  |0          |-72     |0          |-76.43
  --------------------------+-------+-----------+--------+-----------+-----------------
  GRAND TOTAL               |-136.38|-515.38    |-240    |-87.57     |-979.13
  </code></pre>