使用Linq计算组 [英] Counting Using Group By Linq
本文介绍了使用Linq计算组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
注意
{
string name,
字符串地址
}
在列表< Notice> ;
我想输出所有不同的名称以及特定的集合出现次数。
例如:
Notice1.Name =旅行
Notice2.Name =旅行
Notice3.Name =PTO
Notice4Name =Direct
我希望输出
Travel - 2
PTO - 1
直接-1
我可以用这段代码得到不同的名字,但是我似乎无法在1个linq语句中得到所有的计数
theNoticeNames = theData.Notices.Select(c => c.ApplicationName).Distinct()。ToList();
解决方案
var noticesGrouped = notices.GroupBy(n => n.Name)。
选择(group =>
new
{
NoticeName = group.Key,
Notices = group.ToList(),
Count = group。 Count()
});
I have an object that looks like this:
Notice
{
string Name,
string Address
}
In a List<Notice>
I want to output All distinct Name and how many times the particular appears in the collection.
For example:
Notice1.Name="Travel"
Notice2.Name="Travel"
Notice3.Name="PTO"
Notice4.Name="Direct"
I want the output
Travel - 2
PTO - 1
Direct -1
I can get the distinct names fine with this code but I can't seem to get the counts all in 1 linq statement
theNoticeNames= theData.Notices.Select(c => c.ApplicationName).Distinct().ToList();
解决方案
var noticesGrouped = notices.GroupBy(n => n.Name).
Select(group =>
new
{
NoticeName = group.Key,
Notices = group.ToList(),
Count = group.Count()
});
这篇关于使用Linq计算组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文