How to get the Max() of a Count() with LINQ

前端 未结 4 933
面向向阳花
面向向阳花 2021-02-08 20:45

I\'m new to LINQ and I have this situation. I have this table:

ID Date  Range
1 10/10/10 9-10
2 10/10/10 9-10
3 10/10/10 9-10
4 10/10/10 8-9
5 10/11/10 1-2
6 10/         


        
4条回答
  •  庸人自扰
    2021-02-08 21:29

    I think something along these lines should work:

    List items = GetItems();
    var orderedByMax = from i in items
                       group i by i.Date into g
                       let q = g.GroupBy(i => i.Range)
                                .Select(g2 => new {Range = g2.Key, Count = g2.Count()})
                                .OrderByDescending(i => i.Count)
                       let max = q.FirstOrDefault()
                       select new {
                          Date = g.Key,
                          Range = max.Range,
                          Total = max.Count
                       };
    

提交回复
热议问题