Mega Code Archive

 
Categories / C# Book / 04 LINQ
 

0468 GroupBy - Simple 2

public void Linq41() { string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" }; var wordGroups = from w in words group w by w[0] into g select new { FirstLetter = g.Key, Words = g }; foreach (var g in wordGroups) { Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter); foreach (var w in g.Words) { Console.WriteLine(w); } } } Result Words that start with the letter 'b': blueberry banana Words that start with the letter 'c': chimpanzee cheese Words that start with the letter 'a': abacus apple