如何在List <dictionary <string,object =“”>&gt; </dictionary <string,>上执行Linq`s group和sum操作

时间:2013-12-28 10:10:50

标签: c# linq dictionary

我有以下代码。对于objViewForecastTrendYear2中的每个项目,我正在创建创建DicGridRow字典对象。我在DicGridRow字典中动态添加了一些属性以及一些静态属性。最后,我在List的{​​{1}}中添加了该项目。一旦我走出外圈,我就有Dictionary EntireViewModel

此列表包含一些记录,我想在此List<Dictionary> EntireViewModel上使用linq应用group by和sum,但我不知道该怎么做。这里EntireViewModel是包含所有静态属性的列表对象。

objViewForecastTrendYear2中的每个项目的整体情况为EntireViewModel =( A(static properties)+B(Dynamic properties))

我想在objViewForecastTrendYear2上对Linq的和操作中的UOM_ID和剩余字段等所有ID字段应用group by。 我的EntireViewModel看起来像 - &gt; enter image description here

EntireViewModel

您可以看到我的List<Dictionary<string, object>> EntireViewModel = new List<Dictionary<string, object>>(); foreach (var A in objViewForecastTrendYear2) { DynamicColName = new string[NoOfHistoryColumn]; int HistoryCol = RightHistoryColumn; Dictionary<string, object> DicGridRow = new Dictionary<string, object>(); DicGridRow.Add("COMPANY_ID", A.COMPANY_ID); DicGridRow.Add("CUSTOMER_ID", A.CUSTOMER_ID); DicGridRow.Add("FINAL_FORECAST", A.FINAL_FORECAST); DicGridRow.Add("FINANCIAL_YEAR", A.FINANCIAL_YEAR); DicGridRow.Add("FORECAST_MONTH", A.FORECAST_MONTH); DicGridRow.Add("FORECAST_PARAMETERS_ID", A.FORECAST_PARAMETERS_ID); DicGridRow.Add("MARKET_PLANNER", A.MARKET_PLANNER); DicGridRow.Add("PROD_GRADE_ID", A.PROD_GRADE_ID); DicGridRow.Add("PROD_ID", A.PROD_ID); // DicGridRow.Add("SALES_LAST_YEAR", A.SALES_LAST_YEAR); DicGridRow.Add("STATISTICAL_FORECAST", A.STATISTICAL_FORECAST); DicGridRow.Add("STOCK_POINT_ID", A.STOCK_POINT_ID); DicGridRow.Add("TREND_ID", A.TREND_ID); DicGridRow.Add("UOM_ID", A.UOM_ID); DicGridRow.Add("VERSION_NO", A.VERSION_NO); DicGridRow.Add("CUSTOMER_FORECAST",A.CUSTOMER_FORECAST); // Dynamic columns goes as follows for (int i = 0; i < NoOfHistoryColumn; i++) { string strColumnName = Convert.ToString(HistoryCol--); DynamicColName[i] = strColumnName; //Dictionary<string, object> DicGridRow = new Dictionary<string, object>(); DicGridRow.Add(strColumnName, A.SALES_LAST_YEAR); } EntireViewModel.Add(DicGridRow); } 属于List个对象 现在,List的分组必须基于2个或更多字典的键来完成,就像我们在SQL数据库中那样(例如PL-SQL)。

1 个答案:

答案 0 :(得分:0)

在这里,我可以从List<Dictionary> EntireViewModel获取所需数据。但只有静态数据......任何人都可以告诉如何在EntireViewModel

中提供的linq的sum运算中添加动态列
var DynamicQuery = EntireViewModel
      .GroupBy(d =>
           new{
            FINANCIAL_YEAR= d["FINANCIAL_YEAR"],
             CUSTOMER_ID=d["CUSTOMER_ID"],
             PROD_GRADE_ID = d["PROD_GRADE_ID"],
             PROD_ID = d["PROD_ID"],
              UOM_ID = d["UOM_ID"]
              })
             .Select(x => new
               {
           FINANCIAL_YEAR = x.Key.FINANCIAL_YEAR,
          CUSTOMER_ID = x.Key.CUSTOMER_ID,
          PROD_GRADE_ID = x.Key.PROD_GRADE_ID,
           PROD_ID = x.Key.PROD_ID,
          UOM_ID = x.Key.UOM_ID,
       STATISTICAL_FORECAST = x.Sum(r => Convert.ToDecimal(r["STATISTICAL_FORECAST"])),
            CUSTOMER_FORECAST = x.Sum(r => Convert.ToDecimal(r["CUSTOMER_FORECAST"])),
            MARKET_PLANNER = x.Sum(r => Convert.ToDecimal(r["MARKET_PLANNER"])),
            FINAL_FORECAST = x.Sum(r => Convert.ToDecimal(r["FINAL_FORECAST"]))
           // For Dynamic Column ?????
             }
         );