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:
Post your comments / questions
Recent Article
- How to check PAN-Aadhaar is Linked or NOT?
- How to customize pagination for django admin?
- How to fix HAXM is not installed |in Android Studio
- How to fix CMOS Checksum Error in Computer or Laptop | SOLVED
- Reactivating windows after a Hardware change on PC or Laptop
- FIXED: Windows reported that the hardware of your device has changed. Error code :0xc004F211
- "redirect" is not defined pylance("reportUndefinedVariable)
- This action cannot be completed because the file is open in SQL Server(SQLEXPRESS) - FIXED
Related Article