按最新组申请

时间:2018-10-16 05:01:22

标签: c# linq group-by linq-to-entities

请考虑以下表格:

Organization           State          Year          Month        Value
----------------------------------------------------------------------
   O1                   NY             2017          1            1
   01                   WA             2017          1            2
   01                   SA             2017          1            3
   O1                   NY             2017          2            4
   01                   WA             2017          2            5
   01                   SA             2017          2            6
   O2                   NY             2015          9            7
   02                   WA             2015          9            8
   02                   SA             2015          9            9
   O2                   NY             2016          1            10
   02                   WA             2016          1            11
   02                   SA             2016          1            12
   O3                   NY             2017          8            13
   03                   WA             2017          8            14
   03                   SA             2017          8            15

我要创建此结果:

Organization           Year          Month        Sum
------------------------------------------------------
    01                 2017            2           15
    02                 2016            1           33
    03                 2017            8           42

我想对最新的Year, Month进行分组并计算总和。在上面的示例中,组织01拥有2个期间的数据,但我想按最新期间分组。


更新1)

var query = from o in MyList
            group o by new {c.Organization, c.Year , c.Month} int grp
            select new 
            { 
                grp.Key.Organization, 
                grp.Key.Year, 
                grp.Key.Month, 
                grp.Sum() 
            };

2 个答案:

答案 0 :(得分:1)

请尝试以下查询:

class Program
{
    static void Main(string[] args)
    {           

        var results = (from o in MyList
                       group o by new { o.Organization } into g
                       select new
                       {
                           Org_Id = g.Key.Organization,
                           Year = g.Select(x => x.Year)
                                   .Max(),
                           Month = g.Where(x => x.Year == g.Select(y => y.Year).Max())
                                    .Select(z => z.Month)
                                    .Max(),
                           Sum = g.Where(x => x.Month == g.Where(y => y.Year == g.Select(z => z.Year).Max())
                                                          .Select(y => y.Month)
                                                          .Max())
                                  .Select(z => z.Value)
                                  .Sum()
                       }).ToList();

        results.ForEach(x => Console.WriteLine($"Org_Id: {x.Org_Id} \t Year: {x.Year} \t Month: {x.Month} \t Sum: {x.Sum}"));

        Console.ReadLine();
    }
}

我们在查询中所做的事情

1)按Organization分组

2)Org_Id:作为您组的密钥

3)Year:从组中选择Max年。

4)Month:通过从组中选择年份的Max来选择月份的Max

5)Sum:通过从组中选择年份Max的月份的Max得出的总和。

输出:

enter image description here

上述查询的SQL:

SELECT [t1].[Organization] AS [Org_Id], (
    SELECT MAX([t2].[Year])
    FROM [Org] AS [t2]
    WHERE (([t1].[Organization] IS NULL) AND ([t2].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t2].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t2].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t2].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t2].[Organization]))))
    ) AS [Year], (
    SELECT MAX([t3].[Month])
    FROM [Org] AS [t3]
    WHERE ([t3].[Year] = ((
        SELECT MAX([t4].[Year])
        FROM [Org] AS [t4]
        WHERE (([t1].[Organization] IS NULL) AND ([t4].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t4].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t4].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t4].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t4].[Organization]))))
        ))) AND ((([t1].[Organization] IS NULL) AND ([t3].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t3].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t3].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t3].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t3].[Organization])))))
    ) AS [Month], (
    SELECT SUM([t5].[Value])
    FROM [Org] AS [t5]
    WHERE ([t5].[Month] = ((
        SELECT MAX([t6].[Month])
        FROM [Org] AS [t6]
        WHERE ([t6].[Year] = ((
            SELECT MAX([t7].[Year])
            FROM [Org] AS [t7]
            WHERE (([t1].[Organization] IS NULL) AND ([t7].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t7].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t7].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t7].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t7].[Organization]))))
            ))) AND ((([t1].[Organization] IS NULL) AND ([t6].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t6].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t6].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t6].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t6].[Organization])))))
        ))) AND ((([t1].[Organization] IS NULL) AND ([t5].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t5].[Organization] IS NOT NULL) AND ((([t1].[Organization] IS NULL) AND ([t5].[Organization] IS NULL)) OR (([t1].[Organization] IS NOT NULL) AND ([t5].[Organization] IS NOT NULL) AND ([t1].[Organization] = [t5].[Organization])))))
    ) AS [Sum]
FROM (
    SELECT [t0].[Organization]
    FROM [Org] AS [t0]
    GROUP BY [t0].[Organization]
    ) AS [t1]

输出:

enter image description here

答案 1 :(得分:0)

我认为您获取输出的逻辑是错误的。我从“输出”分组中看到的内容分两个步骤完成

第1步

Group By Organization, State, MAX(Year) AS Year, MAX(Month) AS Month, MAX(Value) As Value

第二步

Group By Organization, MAX(Year) AS Year, MAX(Month) AS Month, SUM(Value) As Value

最终输出具有组织在每个州下的最大值的总和

如果我的假设是正确的,那么您将能够为此编写LINQ查询