Grouping by substring : Group « LINQ « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


public class MainClass{

   public static void Main(string[] args){   
         String[] TestData = {"One", "Two", "Three", "Four",
                              "Five", "Six", "Seven", "Eight", 
                              "Nine", "Ten"};

         var ThisQuery = from ThisElement in TestData
                         group ThisElement 
                         by ThisElement.Substring(0, 1)
                         into Groups
                         orderby Groups.Key
                         select Groups;

         foreach (var ThisElement in ThisQuery)
         {
            Console.WriteLine(ThisElement.Key);
            foreach (String ThisText in ThisElement)
               Console.WriteLine(ThisText);
         }
   }
}








22.46.Group
22.46.1.Select from a Group
22.46.2.Group words by length
22.46.3.Using group to group all employees by department
22.46.4.Group Query
22.46.5.Grouping by substring