I have the following code. For every item in objViewForecastTrendYear2
, I'm creating creating DicGridRow
dictionary object. I add some properties dynamically along with some static properties in DicGridRow
dictionary. At the end I am adding that item in List
of Dictionary EntireViewModel
. Once I come out of outer loop ,i have List<Dictionary> EntireViewModel
.
This list contains some record and i want to apply group by and sum using linq on this EntireViewModel
, but I don't know how to do that. Here objViewForecastTrendYear2
is list object which contain all static properties.
Overall scenario is EntireViewModel =( A(static properties)+B(Dynamic properties))
for every item in objViewForecastTrendYear2
.
I want to apply group by on all ID field like UOM_ID and remaining fields in sum operation of Linq on EntireViewModel
. My EntireViewModel look like ->
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);
}
As you can see my List
is of dictionary<string, object>
object Now the grouping of List has to be done based upon 2 or more keys of dictionary as we do in SQL databases (ex. PL-SQL).
Here I am able to get required data from List<Dictionary> EntireViewModel
. But only static data...Anyone can tell how to add dynamic column in sum operation of linq which is available in EntireViewModel
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 ?????
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.