Group words by length : Group « LINQ « C# / CSharp Tutorial






using System;
using System.Linq;

class HelloWorld {
    static void Main() {
        string[] words = { "A", "ss", "w", "ccc", "a" };
        var groups =
          from word in words
          orderby word ascending
          group word by word.Length into lengthGroups
          orderby lengthGroups.Key descending
          select new { Length = lengthGroups.Key, Words = lengthGroups };

        foreach (var group in groups) {
            Console.WriteLine("Words of length " + group.Length);
            foreach (string word in group.Words)
                Console.WriteLine("  " + word);
        }
    }
}








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