linq GroupBy

GroupBy
This sample uses group by to partition a list of words by their first letter.
 public static void GroupBy()
        {
            string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };

            var wordGroups =
                from w in words
                group w by w[0] into g
                select new { FirstLetter = g.Key, Words = g };

            foreach (var g in wordGroups)
            {
                Console.WriteLine("\nWords that start with the letter '{0}':", g.FirstLetter);
                foreach (var w in words)
                {
                    String _result = w[0] == g.FirstLetter ? w : String.Empty;
                    if (_result != String.Empty)
                    {
                        Console.WriteLine("{0}", _result);
                    }
                }
            }
        }


Output
linq GroupBy linq GroupBy Reviewed by Bhaumik Patel on 8:25 PM Rating: 5