Created
March 17, 2023 13:22
-
-
Save savaged/57cc6b78f48d209d72b08dfad77b6bec to your computer and use it in GitHub Desktop.
Simple example of Linq GroupBy in C# using method syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var customers = new List<(string Name, string Town)> | |
{ | |
("Isaac", "London"), | |
("Sara", "Birmingham"), | |
("Tim", "London"), | |
("Michelle", "Manchester") | |
}; | |
var customersByTownQuery = customers | |
.GroupBy(c => c.Town) | |
.OrderBy(g => g.Key) | |
.Select(g => g); | |
foreach (var group in customersByTownQuery) | |
{ | |
Console.WriteLine(group.Key); | |
foreach (var customer in group) | |
{ | |
Console.WriteLine($"\t{customer.Name}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment