LINQ with groupby和count

LINQ with groupby和count

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


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并计算....任何帮助?


POST编辑:我从来没有能够得到发布的答案,因为他们总是返回一个记录与不同的计数。但是,我能够将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方法非常相似,但给了我一个不同的答案。


Qyouu
浏览 416回答 3
3回答

拉风的咖菲猫

调用之后GroupBy,您将获得一系列组IEnumerable<Grouping>,其中每个分组本身都会公开Key用于创建组的组,也是IEnumerable<T>原始数据集中的任何项目。您只需要调用Count()该分组即可获得小计。foreach(var line in data.GroupBy(info => info.metric)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(group => new {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Metric = group.Key,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Count = group.Count()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderBy(x => x.Metric){&nbsp; &nbsp; &nbsp;Console.WriteLine("{0} {1}", line.Metric, line.Count);}这是一个非常快速的回复,但我对第一行有一点问题,特别是“data.groupby(info => info.metric)”我假设你已经有一些class看起来像的列表/数组class UserInfo {&nbsp; &nbsp; string name;&nbsp; &nbsp; int metric;&nbsp; &nbsp; ..etc..}&nbsp;...List<UserInfo> data = ..... ;当你这样做时data.GroupBy(x => x.metric),它意味着“对于xIEnumerable中定义的每个元素data,计算它.metric,然后将具有相同度量的所有元素分组为a Grouping并返回IEnumerable所有结果组。给出你的示例数据集&nbsp; &nbsp; <DATA>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| Grouping Key (x=>x.metric) |joe&nbsp; 1 01/01/2011 5&nbsp; | 1jane 0 01/02/2011 9&nbsp; | 0john 2 01/03/2011 0&nbsp; | 2jim&nbsp; 3 01/04/2011 1&nbsp; | 3jean 1 01/05/2011 3&nbsp; | 1jill 2 01/06/2011 5&nbsp; | 2jeb&nbsp; 0 01/07/2011 3&nbsp; | 0jenn 0 01/08/2011 7&nbsp; | 0在groupby之后会产生以下结果:(Group 1): [joe&nbsp; 1 01/01/2011 5, jean 1 01/05/2011 3](Group 0): [jane 0 01/02/2011 9, jeb&nbsp; 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&nbsp; 3 01/04/2011 1]

HUH函数

假设userInfoList是List<UserInfo>:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;groups&nbsp;=&nbsp;userInfoList&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.GroupBy(n&nbsp;=>&nbsp;n.metric) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(n&nbsp;=>&nbsp;new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MetricName&nbsp;=&nbsp;n.Key, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MetricCount&nbsp;=&nbsp;n.Count() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OrderBy(n&nbsp;=>&nbsp;n.MetricName);对于lambda表达式GroupBy(),n => n.metric意味着它会得到场metric从每一个UserInfo遇到的对象。类型n取决于上下文,在第一次出现时是类型UserInfo,因为列表包含UserInfo对象。在第二次出现n是类型Grouping,因为现在它是一个Grouping对象列表。Grouping有像你这样的扩展方法.Count(),.Key()以及你期望的其他任何东西。正如你查.Lenght一个string,你可以检查.Count()在一组。

呼唤远方

userInfos.GroupBy(userInfo&nbsp;=>&nbsp;userInfo.metric) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.OrderBy(group&nbsp;=>&nbsp;group.Key) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(group&nbsp;=>&nbsp;Tuple.Create(group.key,&nbsp;group.Count()));
打开App,查看更多内容
随时随地看视频慕课网APP