{"id":52,"date":"2011-05-12T14:51:00","date_gmt":"2011-05-12T14:51:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/05\/12\/linq-group-by-and-sort\/"},"modified":"2011-05-12T14:51:00","modified_gmt":"2011-05-12T14:51:00","slug":"linq-group-by-and-sort","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/05\/12\/linq-group-by-and-sort\/","title":{"rendered":"Linq:  Group by and sort"},"content":{"rendered":"<p>The following LINQ statement was the first one I wrote:<\/p>\n<pre><code><br \/>var hsQ = from hspt in hospitals<br \/>          orderby hspt.County, hspt.City<br \/>          group hspt by hspt.County;<br \/><\/code><\/pre>\n<p>It obviously groups by country and then sorts by city.<\/p>\n<p>However, I found the following even better as it groups first by country, sorts by country and then sorts within the group by city:<\/p>\n<pre><code><br \/>var hsQ = from hspt in hospitals<br \/>          orderby hspt.City<br \/>          group hspt by hspt.County into hsptGroup<br \/>          orderby hsptGroup.First().County<br \/>          select hsptGroup;<br \/><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/05\/12\/linq-group-by-and-sort\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Linq:  Group by and sort&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,33],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"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&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}