Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Group By with Count in Linq September 29, 2009

Filed under: C#,Linq — Rupert Bates @ 8:26 pm

To group a sequence getting a count for each element of the grouped sequence using Linq:

            var wordList = new List<String> { "test", "one", "test", "two" };
            var grouped = wordList
                .GroupBy(i => i) //Group the words
                .Select(i => new { Word = i.Key, Count = i.Count() }); //get a count for each

Which results in this sequence:



2 Responses to “Group By with Count in Linq”

  1. hi Says:

    Great man, i used today, amazing stuff !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s