Search This Blog

Monday, August 15, 2011

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;

No comments: