{"id":904,"date":"2012-05-11T14:16:51","date_gmt":"2012-05-11T12:16:51","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=904"},"modified":"2012-05-11T14:16:51","modified_gmt":"2012-05-11T12:16:51","slug":"linq-group-by-multiple-fields","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/05\/linq-group-by-multiple-fields\/","title":{"rendered":"Linq &#8211; group by multiple fields"},"content":{"rendered":"<p>Seems this is an issue that multiple people have asked around at several occasions.<\/p>\n<p>The question is how to use a Linq query where you want to group on more than one &#8216;field&#8217;<\/p>\n<p>Turns out it is really not that hard. Thanks to &#8216;Anonymous&#8217; methods that was also introduced &#8216;yay back&#8217; it is as simple as the following example. Lets say you have a list of values like these:<\/p>\n<blockquote><p>Person 1: Age = 25, Gender = M, HighScore = 123<br \/>\nPerson 2: Age = 21, Gender = F, HighScore = 99<br \/>\nPerson 3: Age = 27, Gender = M, HighScore = 140<br \/>\nPerson 4: Age = 19, Gender = M, HighScore = 131<br \/>\nPerson 5: Age = 25, Gender = M, HighScore = 119<br \/>\nPerson 6: Age = 22, Gender = M, HighScore = 108<br \/>\nPerson 7: Age = 27, Gender = M, HighScore = 152<br \/>\nPerson 8: Age = 21, Gender = M, HighScore = 98<br \/>\nPerson 9: Age = 22, Gender = M, HighScore = 101<br \/>\nPerson 10: Age = 25, Gender = M, HighScore = 167<br \/>\nPerson 11: Age = 22, Gender = F, HighScore = 105<br \/>\n&#8230; etc<\/p><\/blockquote>\n<p>To get a simple report on average high score by Age and Gender you&#8217;ll have to use a query like this:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">foreach<\/span>(<span style=\"color: #0000ff;\">var<\/span> entry <span style=\"color: #0000ff;\">in<\/span> (<span style=\"color: #0000ff;\">from<\/span> p <span style=\"color: #0000ff;\">in<\/span> Persons<br \/>\n<span style=\"color: #0000ff;\">group<\/span> p <span style=\"color: #0000ff;\">by new<\/span> <strong>{ \u00a0Age = p.Age, Gender = p.Gender }<\/strong> <span style=\"color: #0000ff;\">into<\/span> g<br \/>\n<span style=\"color: #0000ff;\">orderby<\/span> g.Key.Age, g.Key.Gender<br \/>\n<span style=\"color: #0000ff;\">select new<\/span><br \/>\n{<br \/>\nAge = g.Key.Age,<br \/>\nGender = g.Key.Gender,<br \/>\nAvg = g.Average(pavg=&gt;p.HighScore)<br \/>\n} ))<\/p>\n<p>{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Console<\/span>.WriteLine(&#8220;Age: {0}, Gender: {1}, Avg: {2}&#8221;, entry.Age, entry.Gender, entry.Avg);<\/p>\n<p>}<\/p><\/blockquote>\n<p>Perhaps it might have simpler if you could simply have stated : &#8220;group p by p.Age, p.Gender&#8221; but unfortunately this is not supported. Then again this allows you to be more creative in defining the anonymous type so you can manipulate the data even more. Perhaps in the next version of Linq&#8230; if there is ever one&#8230;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seems this is an issue that multiple people have asked around at several occasions. The question is how to use a Linq query where you want to group on more than one &#8216;field&#8217; Turns out it is really not that &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/05\/linq-group-by-multiple-fields\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,303,173,172],"class_list":["post-904","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-development","tag-groupby","tag-linq"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=904"}],"version-history":[{"count":2,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/904\/revisions"}],"predecessor-version":[{"id":906,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/904\/revisions\/906"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}