无法获得包含汇总值的独特列表

时间:2019-03-28 19:03:18

标签: c#

我已经发布了这篇文章,但是我试图实现的目标似乎已经丢失,因此重新发布它可以更好地自我解释。

我有一个包含具有不同值的重复产品名称的集合。我的目的是获得一个将这些产品名称相加的列表,以便该列表包含这些重复项的单个记录。

For e.g 

If the list contains 
Product A 100
Product A 200

The result object should contain
Product A 300

因此,如下面的代码所示,我正在将IEnumerablelocationsGrouped传递给该方法。我将按产品名称分组并汇总Emv字段,然后对其进行循环,以便创建类型为List的新列表并将其传递给调用方方法。我在这里看到的问题在代码的下列行Items = group。现在,项目包含原始列表,但不包含总数。因此,内部的foreach循环运行的次数多,这是因为有重复项无法达到我的目的。最后,我需要返回具有非重复值的结果对象,这些值基于上述条件求和。你能告诉我我要去哪里了吗。

 private static List<FirmWideAllocationsViewModel>  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),
                    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 productName = ac.ProductName;
                var emvSum = ac.EmvSum;
                foreach (var elem in ac.Items)
                {
                    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;


        }

1 个答案:

答案 0 :(得分:0)

将其更改为:

    var result = allocationsGrouped
                .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
                .GroupBy(product => product.PRODUCT_NAME)
                .Select(group => {
                    var product = group.First();
                    return new FirmWideAllocationsViewModel()
                    {
                        Hierarchy = new List<string>() { manStratName, product.PRODUCT_NAME },
                        FirmID = product.FIRM_ID,
                        FirmName = product.Item.FIRM_NAME,
                        ManagerStrategyID = product.MANAGER_STRATEGY_ID,
                        ManagerStrategyName = product.MANAGER_STRATEGY_NAME,
                        ManagerAccountClassID = product.MANAGER_ACCOUNTING_CLASS_ID,
                        ManagerAccountingClassName = product.MANAGER_ACCOUNTING_CLASS_NAME,
                        ManagerFundID = product.MANAGER_FUND_ID,
                        ManagerFundName = product.MANAGER_FUND_NAME,
                        Nav = product.NAV,
                        EvalDate = product.EVAL_DATE.HasValue ? product.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty,
                        ProductID = product.PRODUCT_ID,
                        ProductName = product.PRODUCT_NAME,
                        UsdEmv = Math.Round((decimal)product.UsdEmv),
                        GroupPercent = product.GroupPercent,
                        WeightWithEq = product.WEIGHT_WITH_EQ,

                        //assign aggregate Sum here
                        EmvSum = group.Sum(x => x.EMV),
                };
            });