Grouping names by their first character - CSharp LINQ

CSharp examples for LINQ:Group By

Description

Grouping names by their first character

Demo Code




using System;//ww w.j a v  a  2 s .  co  m
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {

        string[] names = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
        var letterGroups = from n in names group n by n[0];
        foreach (var currentLetterGroup in letterGroups)
        {
            Console.WriteLine("\tWords that begin with '{0}':", currentLetterGroup.Key);
            foreach (var name in currentLetterGroup)
            {
                Console.WriteLine("\t{0}", name);
            }
        }
    }
}

Result


Related Tutorials