I am basically trying to group the allocationsGrouped list by productname and summing the Emv field. If you below i have assigned the grouping to var a. Unfortunately the after grouping it only contains ProductName and EmvSum as thats the only two fields added in the Select statement I need all the fields below listed in the foreach (var ac in allocationsGrouped). I would need to loop through a and not allocationsGrouped. So it will be foreach (var ac in a). How do I get the other fields after grouping and summing.
private static void CreateHierarchy(string manStratName, IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationsGrouped, List<FirmWideAllocationsViewModel> result)
{
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
{
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
});
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in allocationsGrouped)
{
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(ac.PRODUCT_NAME + "#" + ac.MANAGER_FUND_ID + ac.PRODUCT_ID + ac.EMV);
item2.FirmID = ac.FIRM_ID;
item2.FirmName = ac.FIRM_NAME;
item2.ManagerStrategyID = ac.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = ac.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = ac.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = ac.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = ac.MANAGER_FUND_ID;
item2.ManagerFundName = ac.MANAGER_FUND_NAME;
item2.Nav = ac.NAV;
item2.EvalDate = ac.EVAL_DATE.HasValue ? ac.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = ac.PRODUCT_ID;
item2.ProductName = ac.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)ac.UsdEmv);
item2.GroupPercent = ac.GroupPercent;
item2.WeightWithEq = ac.WEIGHT_WITH_EQ;
result.Add(item2);
}
}
Added the count and filter on a specific product that has duplicate on the proposed solution . The outer count shows one while the inner count shows 2
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP")
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
{
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
});
var b = a;
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in b)
{
var count = b.Count();
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
{
var count1 = ac.Items.Count();
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(elem.PRODUCT_NAME);
item2.FirmID = elem.FIRM_ID;
item2.FirmName = elem.FIRM_NAME;
item2.ManagerStrategyID = elem.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = elem.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = elem.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = elem.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = elem.MANAGER_FUND_ID;
item2.ManagerFundName = elem.MANAGER_FUND_NAME;
item2.Nav = elem.NAV;
item2.EvalDate = elem.EVAL_DATE.HasValue ? elem.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = elem.PRODUCT_ID;
item2.ProductName = elem.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)elem.UsdEmv);
item2.GroupPercent = elem.GroupPercent;
item2.WeightWithEq = elem.WEIGHT_WITH_EQ;
result.Add(item2);
}
}
return result;