c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Linq

Grouping and sorting using linq in C#?

| | Linq

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.

 

Example:

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

            }

            Console.ReadLine();

        }

 

Output: