Linq: Group by and sort

The following LINQ statement was the first one I wrote:


var hsQ = from hspt in hospitals
orderby hspt.County, hspt.City
group hspt by hspt.County;

It obviously groups by country and then sorts by city.

However, I found the following even better as it groups first by country, sorts by country and then sorts within the group by city:


var hsQ = from hspt in hospitals
orderby hspt.City
group hspt by hspt.County into hsptGroup
orderby hsptGroup.First().County
select hsptGroup;

Author: Pouya Panahy

Microsoft certified DevOps engineer with passion in analysing, designing and implementing solutions for Azure Cloud with hands-on experience in security and quality assurence.

Leave a Reply