简体   繁体   English

Linq 中的多个聚合级别

[英]Multiple Aggregation Levels in Linq

I am not sure what my issue to be called, although I mentioned in the subject as "Multiple Aggregation Levels".尽管我在主题中提到了“多个聚合级别”,但我不确定我的问题应该被称为什么。

I would like to Aggregate different dimensions of the data presented.我想聚合所呈现数据的不同维度。 In this Example, I am trying to get Aggregation data by SalesCode and in Detail Aggregation by AccountId .在此示例中,我试图通过SalesCode获取聚合数据,并通过AccountId获取详细信息聚合。 So basically, I could get the Accounts that were associated to the Sales Aggregation Level.所以基本上,我可以获得与销售聚合级别关联的帐户。

Hence the output I am ought to get should be like this:因此我应该得到的输出应该是这样的:

在此处输入图片说明

My Requirement is to Map the data to the following Class:我的要求是将数据映射到以下类:

public class Earning
{
    public string EntityId
    {
        get;
        set;
    }

    public string EntityName
    {
        get;
        set;
    }

    public string EntityType
    {
        get;
        set;
    }

    public int TradeCount
    {
        get;
        set;
    }

    public int OrderCount
    {
        get;
        set;
    }

    public decimal PrincipalAmount
    {
        get;
        set;
    }

    public decimal GrossBrokerage
    {
        get;
        set;
    }

    public decimal NetBrokerage
    {
        get;
        set;
    }

    public List<Earning> Detail
    {
        get;
        set;
    }
}

From the following data:从以下数据来看:

List<Trade> Trades = new List<Trade>(){ 
            new Trade{
                AccountId = "ACT01", 
                SalesCode = "STEVES", 
                PrincipalAmount = 100, 
                GrossBrokerage = 0.64M,
                NetBrokerage = 0.64M
            }, 
            new Trade{
                AccountId = "ACT02", 
                SalesCode = "STEVES", 
                PrincipalAmount = 100, 
                GrossBrokerage = 0.64M,
                NetBrokerage = 0.64M
            }, 
            new Trade{
                AccountId = "ACT01", 
                SalesCode = "STEVES", 
                PrincipalAmount = 50, 
                GrossBrokerage = 0.32M,
                NetBrokerage = 0.32M
            }, 
            new Trade{
                AccountId = "ACT03", 
                SalesCode = "GRAHAMS", 
                PrincipalAmount = 100, 
                GrossBrokerage = 0.64M,
                NetBrokerage = 0.64M
            }, 
        };

Until now I have tried the following ways in the working I am mentioning below, but I am clueless to get the 2nd Level aggregation, which is by AccountId.到目前为止,我已经在下面提到的工作中尝试了以下方法,但是我对通过 AccountId 获得 2nd Level 聚合一无所知。

DotNetFiddle: https://dotnetfiddle.net/SxGdDD DotNetFiddle: https ://dotnetfiddle.net/SxGdDD

The Trade Class look like this:贸易类看起来像这样:

public class Trade
{
    public string AccountId
    {
        get;
        set;
    }

    public string SalesCode
    {
        get;
        set;
    }

    public decimal PrincipalAmount
    {
        get;
        set;
    }

    public decimal GrossBrokerage
    {
        get;
        set;
    }

    public decimal NetBrokerage
    {
        get;
        set;
    }
}

You need another GroupBy .你需要另一个GroupBy

    var results = (
        from r in Trades
        group r by r.SalesCode
        into g
        select new Earning()
        {
            EntityId = g.Key.ToString(),
            EntityName = g.Key.ToString(),
            TradeCount = g.Count(),
            OrderCount = g.Count(),
            PrincipalAmount = g.Sum(c => c.PrincipalAmount),
            GrossBrokerage = g.Sum(c => c.GrossBrokerage),
            NetBrokerage = g.Sum(c => c.NetBrokerage),
            Detail = g.GroupBy(c=>c.AccountId).Select(c => new Earning()
       // Added GroupBy ---^^
            {
                EntityId = c.Key,
                EntityName = c.Key,
                TradeCount = c.Count(),
                OrderCount = c.Count(),
                PrincipalAmount = c.Sum(p=>p.PrincipalAmount),
                GrossBrokerage =  c.Sum(p=>p.GrossBrokerage),
                NetBrokerage = c.Sum(p=>p.NetBrokerage),

            }).ToList(),
        }).ToList();

    foreach (var item in results)
    {
        Console.WriteLine(item.EntityId);
        Console.WriteLine(string.Format("Total Principal Amount: {0}", item.PrincipalAmount.ToString()));
        Console.WriteLine(string.Format("Total Gross Brokerage Amount: {0}", item.GrossBrokerage.ToString()));
        Console.WriteLine(string.Format("Total Net Brokerage Amount: {0}", item.NetBrokerage.ToString()));

        foreach (Earning detail in item.Detail)
        {
            Console.WriteLine(string.Format("-- Detail {0}/{1}", detail.EntityId, detail.EntityName));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM