Ordering the groups - CSharp LINQ

CSharp examples for LINQ:order by

Description

Ordering the groups

Demo Code



using System;//from www .  ja v  a 2 s  .  c  o  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 wordGroups = from n in names
                         orderby n[0]      
                         group n by n[0];  
        foreach (var letterGroup in wordGroups)
        {
            Console.WriteLine("\t\tWords that begin with '{0}': ", letterGroup.Key);
            foreach (var word in letterGroup)
            {
                Console.WriteLine("\t\t\t{0}", word);
            }
        }
    }
}

Result


Related Tutorials