LINQ 与 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 和计数... 有什么帮助吗?
帖子编辑:我永远无法让发布的答案发挥作用,因为它们总是返回一条记录,其中包含不同计数的数量。不过,我能够组合一个确实有效的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用
GroupBy
后,您会得到一系列组IEnumerable
,其中每个 Grouping 本身公开用于创建组的Key
是原始数据集中所有项目的IEnumerable
。您只需对该分组调用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
的列表/数组,看起来像当您执行
data.GroupBy(x => x.metric)
时,这意味着“对于data
定义的 IEnumerable 中的每个元素x
,计算其.metric
,然后将具有相同度量的所有元素分组到一个分组
并返回所有结果组的IEnumerable
给出的示例数据集将在 groupby 后产生以下结果:
After calling
GroupBy
, you get a series of groupsIEnumerable<Grouping>
, where each Grouping itself exposes theKey
used to create the group and also is anIEnumerable<T>
of whatever items are in your original data set. You just have to callCount()
on that Grouping to get the subtotal.> 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 likeWhen you do
data.GroupBy(x => x.metric)
, it means "for each elementx
in the IEnumerable defined bydata
, calculate it's.metric
, then group all the elements with the same metric into aGrouping
and return anIEnumerable
of all the resulting groups. Given your example data set ofit would result in the following result after the groupby:
假设
userInfoList
是List
:GroupBy()
的 lambda 函数,n => n.metric
意味着它将从遇到的每个UserInfo
对象中获取字段metric
。n
的类型取决于上下文,第一次出现时其类型为UserInfo
,因为列表包含UserInfo
对象。在第二次出现时,n
的类型为Grouping
,因为现在它是一个Grouping
对象的列表。Grouping
具有诸如.Count()
、.Key()
之类的扩展方法以及几乎任何您期望的其他方法。正如您在字符串
上检查.Length
一样,您可以在组上检查.Count()
。Assuming
userInfoList
is aList<UserInfo>
:The lambda function for
GroupBy()
,n => n.metric
means that it will get fieldmetric
from everyUserInfo
object encountered. The type ofn
is depending on the context, in the first occurrence its of typeUserInfo
, because the list containsUserInfo
objects. In the second occurrencen
is of typeGrouping
, because now its a list ofGrouping
objects.Grouping
s have extension methods like.Count()
,.Key()
and pretty much anything else you would expect. Just as you would check.Length
on astring
, you can check.Count()
on a group.