LINQ 与 groupby 和 count

发布于 2024-12-02 19:41:11 字数 840 浏览 0 评论 0原文

这很简单,但我不知所措: 给定这种类型的数据集:

UserInfo(name, metric, day, other_metric)

以及此示例数据集:

joe  1 01/01/2011 5
jane 0 01/02/2011 9
john 2 01/03/2011 0
jim  3 01/04/2011 1
jean 1 01/05/2011 3
jill 2 01/06/2011 5
jeb  0 01/07/2011 3
jenn 0 01/08/2011 7

我想检索一个表,该表按顺序(0,1,2,3..)列出指标以及计数发生的总次数。因此,从这组中,您最终会得到:

0 3    
1 2    
2 2    
3 1

我正在努力解决 LINQ 语法,但我不知道在哪里放置 groupby 和计数... 有什么帮助吗?

帖子编辑:我永远无法让发布的答案发挥作用,因为它们总是返回一条记录,其中包含不同计数的数量。不过,我能够组合一个确实有效的 LINQ to SQL 示例:

var pl = from r in info
         orderby r.metric    
         group r by r.metric into grp
         select new { key = grp.Key, cnt = grp.Count()};

这个结果为我提供了一组有序的记录,其中包含“指标”以及与每个记录关联的用户数量。一般来说,我显然对 LINQ 很陌生,在我未经训练的眼中,这种方法似乎与纯 LINQ 方法非常相似,但却给了我不同的答案。

This is pretty simple but I'm at a loss:
Given this type of data set:

UserInfo(name, metric, day, other_metric)

and this sample data set:

joe  1 01/01/2011 5
jane 0 01/02/2011 9
john 2 01/03/2011 0
jim  3 01/04/2011 1
jean 1 01/05/2011 3
jill 2 01/06/2011 5
jeb  0 01/07/2011 3
jenn 0 01/08/2011 7

I'd like to retrieve a table that lists metrics in order(0,1,2,3..) with the total number of times the count occurs. So from this set, you'd end up with:

0 3    
1 2    
2 2    
3 1

I'm grappling with the LINQ syntax but am stuck on where to put a groupby and count...
any help?

POST Edit: I was never able to get the posted answers to work as they always returned one record with the number of different counts. However, I was able to put together a LINQ to SQL example that did work:

var pl = from r in info
         orderby r.metric    
         group r by r.metric into grp
         select new { key = grp.Key, cnt = grp.Count()};

This result gave me an ordered set of records with 'metrics' and the number of users associated with each. I'm clearly new to LINQ in general and to my untrained eye this approach seems very similar to the pure LINQ approach yet gave me a different answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄想挽回 2024-12-09 19:41:11

调用 GroupBy 后,您会得到一系列组 IEnumerable,其中每个 Grouping 本身公开用于创建组的 Key是原始数据集中所有项目的 IEnumerable。您只需对该分组调用 Count() 即可获取小计。

foreach(var line in data.GroupBy(info => info.metric)
                        .Select(group => new { 
                             Metric = group.Key, 
                             Count = group.Count() 
                        })
                        .OrderBy(x => x.Metric))
{
     Console.WriteLine("{0} {1}", line.Metric, line.Count);
}

> This was a brilliantly quick reply but I'm having a bit of an issue with the first line, specifically "data.groupby(info=>info.metric)"

我假设您已经有一些 class 的列表/数组,看起来像

class UserInfo {
    string name;
    int metric;
    ..etc..
} 
...
List<UserInfo> data = ..... ;

当您执行 data.GroupBy(x => x.metric) 时,这意味着“对于 data 定义的 IEnumerable 中的每个元素 x,计算其 .metric,然后将具有相同度量的所有元素分组到一个 分组并返回所有结果组的 IEnumerable 给出的示例数据集

    <DATA>           | Grouping Key (x=>x.metric) |
joe  1 01/01/2011 5  | 1
jane 0 01/02/2011 9  | 0
john 2 01/03/2011 0  | 2
jim  3 01/04/2011 1  | 3
jean 1 01/05/2011 3  | 1
jill 2 01/06/2011 5  | 2
jeb  0 01/07/2011 3  | 0
jenn 0 01/08/2011 7  | 0

将在 groupby 后产生以下结果:

(Group 1): [joe  1 01/01/2011 5, jean 1 01/05/2011 3]
(Group 0): [jane 0 01/02/2011 9, jeb  0 01/07/2011 3, jenn 0 01/08/2011 7]
(Group 2): [john 2 01/03/2011 0, jill 2 01/06/2011 5]
(Group 3): [jim  3 01/04/2011 1]

After calling GroupBy, you get a series of groups IEnumerable<Grouping>, where each Grouping itself exposes the Key used to create the group and also is an IEnumerable<T> of whatever items are in your original data set. You just have to call Count() on that Grouping to get the subtotal.

foreach(var line in data.GroupBy(info => info.metric)
                        .Select(group => new { 
                             Metric = group.Key, 
                             Count = group.Count() 
                        })
                        .OrderBy(x => x.Metric))
{
     Console.WriteLine("{0} {1}", line.Metric, line.Count);
}

> This was a brilliantly quick reply but I'm having a bit of an issue with the first line, specifically "data.groupby(info=>info.metric)"

I'm assuming you already have a list/array of some class that looks like

class UserInfo {
    string name;
    int metric;
    ..etc..
} 
...
List<UserInfo> data = ..... ;

When you do data.GroupBy(x => x.metric), it means "for each element x in the IEnumerable defined by data, calculate it's .metric, then group all the elements with the same metric into a Grouping and return an IEnumerable of all the resulting groups. Given your example data set of

    <DATA>           | Grouping Key (x=>x.metric) |
joe  1 01/01/2011 5  | 1
jane 0 01/02/2011 9  | 0
john 2 01/03/2011 0  | 2
jim  3 01/04/2011 1  | 3
jean 1 01/05/2011 3  | 1
jill 2 01/06/2011 5  | 2
jeb  0 01/07/2011 3  | 0
jenn 0 01/08/2011 7  | 0

it would result in the following result after the groupby:

(Group 1): [joe  1 01/01/2011 5, jean 1 01/05/2011 3]
(Group 0): [jane 0 01/02/2011 9, jeb  0 01/07/2011 3, jenn 0 01/08/2011 7]
(Group 2): [john 2 01/03/2011 0, jill 2 01/06/2011 5]
(Group 3): [jim  3 01/04/2011 1]
一刻暧昧 2024-12-09 19:41:11

假设 userInfoListList

var groups = userInfoList.GroupBy(n => n.metric)
                         .Select(n => new
                          {
                               MetricName = n.Key,
                               MetricCount = n.Count()
                          })
                         .OrderBy(n => n.MetricName);

GroupBy() 的 lambda 函数,n => n.metric 意味着它将从遇到的每个 UserInfo 对象中获取字段 metricn 的类型取决于上下文,第一次出现时其类型为 UserInfo,因为列表包含 UserInfo 对象。在第二次出现时,n 的类型为 Grouping,因为现在它是一个 Grouping 对象的列表。

Grouping 具有诸如 .Count().Key() 之类的扩展方法以及几乎任何您期望的其他方法。正如您在字符串上检查.Length一样,您可以在组上检查.Count()

Assuming userInfoList is a List<UserInfo>:

var groups = userInfoList.GroupBy(n => n.metric)
                         .Select(n => new
                          {
                               MetricName = n.Key,
                               MetricCount = n.Count()
                          })
                         .OrderBy(n => n.MetricName);

The lambda function for GroupBy(), n => n.metric means that it will get field metric from every UserInfo object encountered. The type of n is depending on the context, in the first occurrence its of type UserInfo, because the list contains UserInfo objects. In the second occurrence n is of type Grouping, because now its a list of Grouping objects.

Groupings have extension methods like .Count(), .Key() and pretty much anything else you would expect. Just as you would check .Length on a string, you can check .Count() on a group.

对不⑦ 2024-12-09 19:41:11
userInfos.GroupBy(userInfo => userInfo.metric)
        .OrderBy(group => group.Key)
        .Select(group => Tuple.Create(group.Key, group.Count()));
userInfos.GroupBy(userInfo => userInfo.metric)
        .OrderBy(group => group.Key)
        .Select(group => Tuple.Create(group.Key, group.Count()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文