Skip to content

Instantly share code, notes, and snippets.

@savaged
Created March 17, 2023 13:22
Show Gist options
  • Save savaged/57cc6b78f48d209d72b08dfad77b6bec to your computer and use it in GitHub Desktop.
Save savaged/57cc6b78f48d209d72b08dfad77b6bec to your computer and use it in GitHub Desktop.
Simple example of Linq GroupBy in C# using method syntax
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