Click here to Skip to main content
15,923,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

Learn to DIY from here[^]
 
Share this answer
 
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.
 
Share this answer
 
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 :)
 
Share this answer
 
Comments
Carl Mailey 4-Jul-14 6:32am    
Thanks this is exactly what i wanted.
Marcin Kozub 4-Jul-14 6:35am    
Glad I could help you :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900