LINQ 与 groupby 和计数 [英] LINQ with groupby and count

查看:35
本文介绍了LINQ 与 groupby 和计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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..) 的顺序列出指标以及计数发生的总次数.所以从这个集合中,你会得到:

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

我正在努力解决 LINQ 语法,但我一直在纠结在哪里放置 groupby 并计数...有什么帮助吗?

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

POST 我永远无法让发布的答案起作用,因为它们总是返回一个具有不同计数数量的记录.但是,我能够将一个有效的 LINQ to SQL 示例放在一起:

POST 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()};

此结果为我提供了一组有序的记录,其中包含指标"以及与每个相关联的用户数量.总的来说,我显然是 LINQ 的新手,在我未经训练的眼睛看来,这种方法似乎与纯 LINQ 方法非常相似,但给了我不同的答案.

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.

推荐答案

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

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);
}


> 这是一个非常快速的回复,但我对第一行有点问题,特别是data.groupby(info=>info.metric)"

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


> 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 = ..... ;

当你做data.GroupBy(x => x.metric)时,它的意思是对于data定义的IEnumerable中的每个元素x,计算它的 .metric,然后将所有具有相同度量的元素分组到一个 Grouping 中,并返回所有元素的 IEnumerable结果组.鉴于您的示例数据集

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

在groupby之后会产生以下结果:

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]

这篇关于LINQ 与 groupby 和计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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