[Solved] I want to know the count of each item in a list


You can use GroupBy:

var voteGroups = Votes.GroupBy(s => s);

Now, if you want to know the count of each string use Enumerable.Count:

foreach(var voteGroup in voteGroups)
    Console.WriteLine("Vote:{0} Count:{1}", voteGroup.Key, voteGroup.Count());

Another way is using ToLookup:

var voteLookup = Votes.ToLookup(s => s);
foreach (var voteGroup in voteLookup)
    Console.WriteLine("Vote:{0} Count:{1}", voteGroup.Key, voteGroup.Count());

The lookup has it’s advantages, it enables you to find specific elements like a dictionary. So you could get the “pepsi”-count in this way:

int pepsiCount = voteLookup["Pepsi"].Count();

This does not cause an exception if there is no such string in the list(count will be 0).

if you want to make it case insensitive, so treat “pepsi” and “Pepsi” as equal:

var voteLookup = Votes.ToLookup(s => s, StringComparer.InvariantCultureIgnoreCase);

0

solved I want to know the count of each item in a list