Grouping and sorting using linq in C#?

Grouping and sorting using linq in C#?, someone asked me to explain?

Linq is very easy to read and simplifies the code. In this example we will learn about grouping and sorting using linq. Here we are filtering and Listing the group of words by string length from a string array.



    static void Main(string[] args)


            string[] words = { "Frequency", "wonderful", "Honeypot", "Halftone", "Hyperlink", "Navigation", "Footer" };

            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);






Grouping and sorting using linq in

Post your comments / questions