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

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

问题描述

这是pretty简单,但我很茫然:
鉴于这种类型的数据集

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

我想检索列出顺序指标表(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<分组> ,其中每个分组本身暴露了用于创建组,也是一个的IEnumerable< T> 任何的项目是在原始的数据集。你只需要调用计数()上分组来获得小计。

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.metric)

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 =&GT; x.metric),它意味着每个元素 X 在了IEnumerable由数据定义,计算它的 .metric ,然后组都具有相同的指标为要素一个分组并返回的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天全站免登陆