根据分组求和的值

时间:2019-03-26 19:39:17

标签: c# linq

我有一个列表集合,需要找到具有相同值的ProductName并将列表中的EMV求和。

var result = new List<FirmWideAllocationsViewModel>();

foreach (var ag in allocationsGrouped)
{
    var allocationsGroup = ag.Select(a => a).OrderByDescending(o=> o.UsdEmv ) ;

    CreateHierarchy(ag.Key, allocationsGroup, result);
}

result.GroupBy(x => x.ProductName);
private AllocationsViewModel GetAllocationsViewModel(int id, DateTime date)
{
    var ms = GetStrategy(id);

    DateTime d = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
    if (ms.FIRM_ID != null)
    {
        var firm = GetService<FIRM>().Get(ms.FIRM_ID.Value);
        var currentEntity = new EntityAllocationsViewModel(new EntityViewModel { EntityId = firm.ID, EntityName = firm.NAME, EntityType = EntityType.Firm });
        //var allocationsGrouped = Mapper.Map<List<FIRMWIDE_MANAGER_ALLOCATION>, List<FirmWideAllocationsViewModel>>(GetAllocationsGrouped(EntityType.Firm, firm.ID, d).ToList())

        //Mapper.Map<ILookup<string,FIRMWIDE_MANAGER_ALLOCATION>,ILookup<string,FirmWideAllocationsViewModel>>((ILookup<string, FIRMWIDE_MANAGER_ALLOCATION>)

        var allocationsGrouped = GetAllocationsGrouped(EntityType.Firm, firm.ID, d);
        var result = new List<FirmWideAllocationsViewModel>();

        foreach (var ag in allocationsGrouped)
        {
            var allocationsGroup = ag.Select(a => a).OrderByDescending(o => o.UsdEmv);

            CreateHierarchy(ag.Key, allocationsGroup, result);
        }

        result.GroupBy(x => x.ProductName);

        var missingProducts = Mapper.Map<List<MISSING_PRODUCT>, List<MissingProductsViewModel>>(GetMissingProducts()).GroupBy(a => a.ProductType);
        var chartData = Mapper.Map<List<FIRMWIDE_MANAGER_ALLOCATION>, List<FirmWideAllocationsViewModel>>(GetAllocationsCalculateOther(EntityType.Firm, firm.ID, d));

        var vm = new AllocationsViewModel
        {
            CurrentEntity = currentEntity,
            ManagerAllocations = result,
            MissingProducts = missingProducts,
            ChartData = chartData
        };

        return vm;
    }

    return null;
}


private static void CreateHierarchy(string manStratName, IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationsGrouped, List<FirmWideAllocationsViewModel> result)
{
    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("dd MMM, 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);
    }
}

型号

public class FirmWideAllocationsViewModel
{
    public List<string> Hierarchy
    { get; set; }

    private decimal? _groupPercent;
    public int FirmID { get; set; }
    public string FirmName { get; set; }
    public int? ManagerStrategyID { get; set; }
    public int? ManagerFundID { get; set; }
    public int ManagerAccountClassID{ get; set; }
    public int? ManagerFundOrClassID { get; set; }
    public string ManagerFundName { get; set; }
    public string ManagerAccountingClassName { get; set; }
    public string ManagerStrategyName { get; set; }
    public int? ProductID { get; set; }
    public string ProductName { get; set; }

    public int? Quantity { get; set; }
    public decimal? Nav { get; set; }

    public string EvalDate { get; set; }
    public int? DefaultStrategyID { get; set; }
    public string DEFAULT_STRATEGY_NAME { get; set; }
    public decimal Usd_Emv { get; set; }

    //needed for kendo aggregates
    public decimal UsdEmv { get; set; }

    public decimal Emv { get; set; }
    public decimal? Weight { get; set; }
    public decimal? WeightWithEq { get; set; }
    [NotMapped]
    public decimal? Percent { get; set; } // manual calc

    [NotMapped]
    public decimal? GroupPercent
    {
        get { return _groupPercent; }
        set { _groupPercent = value; }
    }

}

1 个答案:

答案 0 :(得分:1)

让我们为您尝试做的事做一个较小的例子。

型号:

public class Product
{
   public string ProductName { get; set; }
   public decimal Emv { get; set; }
}

最后,您要找到的是每个EmvProductName 的总和。

public Dictionary<string, decimal> SumProductEmv(IEnumerable<Product> allProducts)
{
    return allProducts
        .GroupBy(product => product.ProductName)
        .Select(group => new
        {
            ProductName = group.Key, // this is the value you grouped on - the ProductName
            EmvSum = group.Sum(item => item.Emv)
        })
        .ToDictionary(x => x.ProductName, x => x.EmvSum);
}

这里的窍门是执行Sum操作时,它与group相对。

在此示例中,为方便起见,我将结果打包到字典中。密钥是产品的名称,值是具有该名称的产品的所有Emv值的总和。