Mega Code Archive

 
Categories / VB.Net / Data Structure
 

Using Group By to partition a list of words by first letter

Imports System.IO Imports System.Reflection Imports System.Linq Imports System.Xml.Linq Public Class MainClass    Public Shared Sub Main         Dim words = New String() {"b", "c", "a", "b", "a", "d"}         Dim wordGroups = From word In words _                          Group word By Key = word(0) Into Group _                          Select FirstLetter = Key, WordGroup = Group         For Each g In wordGroups             Console.WriteLine("Words that start with the letter '" & g.FirstLetter & "':")             For Each w In g.WordGroup                 Console.WriteLine(w)             Next         Next    End Sub End Class