在for循环期间创建统计信息 [英] Creating a statistics during a for loop

查看:77
本文介绍了在for循环期间创建统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



好​​的,所以我不知道该把它作为这个的标题。



基本上我循环遍历一个类型的List,每个类型都有一个用户名值,我希望得到每个这些用户名的计数。但是这些用户名的数量和价值是未知的,可以改变。在循环之后我想显示用户名及其出现次数。



完成此任务的最佳方法是什么?

<
问候

Carl

解决方案

此处 [ ^ ]

使用字典< string,>

如果字符串存在于 Dictionary 中,然后递增 int 值。另一方面,如果字符串尚不存在,则在 Dictionary 中创建条目并设置其 int 值为 1


我认为您可以使用LINQ和GroupBy。看看这里的例子:



http:/ /code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b [ ^ ]



特别是部分聚合运算符 - 计数分组



我希望它可以帮助你:)


Hey guys,

Ok so i wasn't sure what to put as a title for this one.

Basically i am looping through a List of a type, each of these types has a username value in it and i want to get a count of each of these usernames as i go through. But the amount of and value of these usernames is unknown and can change. After the loop i want to display the username alongside a count of its occurrence.

What is the best approach to accomplish this?

Regards
Carl

解决方案

Learn to DIY from here[^]


Use a Dictionary<string,>.
If the string exists in the Dictionary then increment the int value. On the other hand, if the string doesn't yet exist, then create the entry in the Dictionary and set its int value to 1.


I think that you can use LINQ and GroupBy. Look at examples here:

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b[^]

Especially section Aggregate Operators - Count grouped

I hope it help you :)


这篇关于在for循环期间创建统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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