MVVC的新增功能需要帮助汇总我模型中的每个循环 [英] New to MVVC need help subtotaling within a for each loop in my model

查看:128
本文介绍了MVVC的新增功能需要帮助汇总我模型中的每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的帮助,每个循环将两个字段相加三分之一.

该模型是UnrecognizedOIModel,其中包含以下字段:

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");
    }

我需要按PromoGroup汇总案件和责任.我是字典和选择器等的新手.

我当前的DataModel看起来像这样:

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)
      {
        //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;




谢谢您的回答,但没有成功.

我在UnrecognizedOISSubtotal上出现错误




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;
    }



现在我在报告cshtml页面时遇到问题,如下所示:



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

推荐答案

那并不太奏效,我尝试过的是:

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>


这篇关于MVVC的新增功能需要帮助汇总我模型中的每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆