使用c#linq分组和求和后包括所有字段

时间:2019-03-27 16:47:34

标签: c# linq

我基本上是在尝试按产品名称对allocationsGrouped列表进行分组,并对Emv字段求和。如果您在下面,我已将分组分配给var a。不幸的是,在分组之后,它仅包含ProductName和EmvSum,因为多数民众赞成在Select语句中仅添加了两个字段 我需要在foreach中有以下所有字段(allocationsGrouped中的var ac)。我将需要遍历a而不是循环分配。因此它将是foreach(a中的var ac)。分组和求和后如何获取其他字段。

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

对在建议的解决方案上重复的特定产品添加了计数和过滤器。外部计数显示1,内部计数显示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;

1 个答案:

答案 0 :(得分:0)

IGrouping<TKey, TElement>类型是IEnumerable<TElement>。这意味着您只需要选择group以便进一步使用。因此,代码的相关部分将是

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

请注意Items = group中的Select

您现在可以按以下方式访问已分组的项目

foreach (var ac in a) {
    var productName = ac.ProductName;
    var emvSum = ac.EmvSum;
    foreach (var elem in ac.Items) {
        // elem is your FIRMWIDE_MANAGER_ALLOCATION in this group
    }
}

测试 您可以使用以下代码进行测试

var allocationsGrouped = new List<Product>() {
    new Product { PRODUCT_NAME = "T1", EMV = 1 },
    new Product { PRODUCT_NAME = "T2", EMV = 2 },
    new Product { PRODUCT_NAME = "T1", EMV = 3 },
    new Product { PRODUCT_NAME = "T2", EMV = 4 },
    new Product { PRODUCT_NAME = "T1", EMV = 5 },
};

您会注意到,这将构成以下数据

"a": 
[
    [
        "ProductName": "T1"
        "EmvSum": 9,
        "Items": 
        [
            [{"EMV": 1, "PRODUCT_NAME": "T1"}],
            [{"EMV": 3, "PRODUCT_NAME": "T1"}],
            [{"EMV": 5, "PRODUCT_NAME": "T1"}]
        ], 
    ], 
    [
        "ProductName": "T2"
        "EmvSum": 6,
        "Items": 
        [
            [{"EMV": 2, "PRODUCT_NAME": "T2"}],
            [{"EMV": 4, "PRODUCT_NAME": "T2"}],
        ], 
    ] 
]