Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help with a simple for each loop to subtotal two fields by a third.

The model is UnrecognizedOIModel which contains the following fields:
C#
public UnrecognizedOIModel(DataRow row)
    {
      Invoice = row.Field<string>("InvoiceNbr").Trim();
      InvoiceDate = row.Field<DateTime>("InvoiceDate");
      Customer = row.Field<string>("BillName").Trim();
      ProductGroup = row.Field<string>("ProductGroup");
      GroupDescription = row.Field<string>("Descr");
      PromoGroup = row.Field<string>("PlanDescr");
      Cases = row.Field<double>("QtyShip");
      PlanKey = row.Field<string>("AR_PlanKey").Trim();
      Allowance = row.Field<double>("AllowanceAmt");
      Liability = row.Field<double>("Liability");
    }
I need to sum Cases and Liability by PromoGroup. I am new to dictionaries and selectors etc.

My current DataModel looks like this:
C#
public class UnrecognizedOIsData
  {
    public UnrecognizedOIsArgs Args { get; set; }   
    public List<UnrecognizedOIModel> UnrecognizedOIs { get; set; }
    public SelectorModel CustSelector { get; set; }
    public SelectorModel PromoSelector { get; set; }
    public Dictionary<string, UnrecognizedOIsSubTotal> GetSubTotals()
    {
      var d = new Dictionary<string, UnrecognizedOIsSubTotal>();
      foreach (var row in UnrecognizedOIs)
      {
        //For each object in row find promogroup in dictionary
        //if not there add promogroup to dictionary
        //Add Cases and Liability to sums
       
       
      }
      return d;
    }
  }
  public class UnrecognizedOIsSubTotal
  {
    public int TotCases { get; set; }
    public double TotLiability { get; set; }
  }
The selectors are used in a page where I have already shown a table by customer. Now I need to total that data by promo group.

Help please!

What I have tried:

foreach (var row in UnrecognizedOIs)
      {
        //For each object in row find promogroup in dictionary
        //if not there add promogroup to dictionary
        //Add Cases and Liability to sums
       
        var PromoGroup = row.PromoGroup;

        if (!d.ContainsKey(row.PromoGroup))
        {
          d.Add(PromoGroup, <UnrecognizedOIsSubTotal> row.Cases, row.Liability);
        }
          d[PromoGroup].  new Dictionary<string, UnrecognizedOIsSubTotal>();
      }
      return d;



Thank you for the answer but it didn't work.

I got errors on the UnrecognizedOISSubtotal

d.Add(PromoGroup, <UnrecognizedOIsSubTotal> row.Cases, row.Liability);
        }
          d[PromoGroup].  new Dictionary<string, UnrecognizedOIsSubTotal>();


So I did this:

var PromoGroup = row.PromoGroup;

      if (!d.ContainsKey(row.PromoGroup))
      {
        d.Add(PromoGroup, new UnrecognizedOIsSubTotal());
      }
      d[PromoGroup].TotCases = d[PromoGroup].TotCases + row.Cases;
      d[PromoGroup].TotLiability = d[PromoGroup].TotLiability + row.Liability;
    }


Now I am having issues with the report cshtml page looks like this:
      {
        var subTotals = Model.GetSubTotals();
        var promoKey = subTotals.Keys.OrderBy(k => k);
      }

      foreach (var promoKey in subTotals[PromoGroup].Keys.OrderBy(k => k))
        {
          var sub = subTotals[promoKey];
          
            
            
            
          
        }      
    
  <table class="report"><thead><tr><th>Promo Group</th><th>Cases</th><th>Liability</th></tr></thead><tbody><tr><td>sub.promoKey</td><td>sub.Cases</td><td>sub.Liability</td></tr></tbody></table>

No error but when it runs I get a printout of the for each loop on the html page
Posted
Updated 10-Apr-17 10:21am
v3

1 solution

That didn't quite work what I did try is this:

<table class="report">
   <thead>
     <tr>
       <th>Promo Group</th>
       <th>Cases</th>
       <th>Liability</th>
     </tr>
   </thead>
   <tbody>
     {
       var subTotals = Model.GetSubTotals();
       var promoKey = subTotals.Keys.OrderBy(k => k);
     }

     foreach (var promoKey in subTotals[PromoGroup].Keys.OrderBy(k => k))
       {
         var sub = subTotals[promoKey];
         <tr>
           <td>sub.promoKey</td>
           <td>sub.Cases</td>
           <td>sub.Liability</td>
         </tr>
       }
   </tbody>
 </table>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900