Ordering within the groups - CSharp LINQ

CSharp examples for LINQ:order by

Description

Ordering within the groups

Demo Code




using System;//w w  w.j a  va 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]      // Sort the names by their first letter.
                         group n by n[0];  // Group them by their first letter.
        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);
            }
        }
        foreach (var letterGroup in wordGroups)
        {
            Console.WriteLine("\tWords that begin with '{0}':", letterGroup.Key);
            var words = from g in letterGroup
                        orderby g
                        select g;

            foreach (var word in words)
            {
                Console.WriteLine("\t\t{0}", word);
            }
        }
    }
}

Result


Related Tutorials