如何使用LINQ获得Count()的Max() [英] How to get the Max() of a Count() with LINQ

查看:71
本文介绍了如何使用LINQ获得Count()的Max()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是LINQ的新手,我遇到了这种情况.我有这张桌子:

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/11/10 1-2
7 10/12/10 5-6

我只想按范围列出每个日期的最大行数,如下所示:

I just want to list the Maximun of rows per date by range, like this:

Date  Range  Total
10/10/10 9-10  3
10/11/10 1-2  2
10/12/10 5-6  1

我想使用LINQ来做到这一点,您对如何做到这一点有任何想法吗?

I want to do this by using LINQ, do you have any ideas of how to do this?

推荐答案

我认为应遵循以下原则:

I think something along these lines should work:

List<MyTable> 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
                   };

这篇关于如何使用LINQ获得Count()的Max()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆