GroupBy and count the unique elements in a List

.net, c#

Solution

var list = new List<string> { "Foo1", "Foo2", "Foo3", "Foo2", "Foo3", "Foo3", "Foo1", "Foo1" };

var grouped = list
    .GroupBy(s => s)
    .Select(group => new { Word = group.Key, Count = group.Count() });

Problem

I have a list that contains only strings. What I would love to do is group by and return a count. For instance: ``` Foo1 Foo2 Foo3 Foo1 Foo2 Foo2 ``` Would result in Foo1: 2, Foo2: 3, Foo3: 1. I've tried with Linq but the list has a GroupBy that might do the trick but i messed it up, can't figure the use :(

Original source

Related problems