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:
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:
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)
{
}
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)
{
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