{"id":63,"date":"2010-03-25T12:43:00","date_gmt":"2010-03-25T12:43:00","guid":{"rendered":"https:\/\/pagan-gerbil.net\/?p=63"},"modified":"2010-03-25T12:43:00","modified_gmt":"2010-03-25T12:43:00","slug":"linq","status":"publish","type":"post","link":"https:\/\/www.pagan-gerbil.net\/?p=63","title":{"rendered":"Linq"},"content":{"rendered":"<p>I\u2019ve just found out how to combine .Sum() with a where clause, and all it took was a further step into the murky world of Linq.<\/p>\n<p>I got my tip-off from a fellow BlogEngine.NET blog user Gary Kilminster &#8211; <a title=\"http:\/\/cunningplan.co.uk\/post\/Extension-Methods.aspx\" href=\"http:\/\/cunningplan.co.uk\/post\/Extension-Methods.aspx\">http:\/\/cunningplan.co.uk\/post\/Extension-Methods.aspx<\/a> \u2013 although it doesn\u2019t look like his blog has been updated for a while.<\/p>\n<p>I had a list of custom objects, and wanted to get the sum of all their \u2018Cost\u2019 values as long as they had a specific property in common. The bit of Gary\u2019s code that really made it click for me was:<\/p>\n<pre class=\"code\"><span style=\"background: #f0f0f0\">        <\/span><span style=\"background: #f0f0f0; color: blue\">string <\/span><span style=\"background: #f0f0f0\">londonNames = (<\/span><span style=\"background: #f0f0f0; color: blue\">from <\/span><span style=\"background: #f0f0f0\">c <\/span><span style=\"background: #f0f0f0; color: blue\">in <\/span><span style=\"background: #f0f0f0\">db.Customers\n<\/span><span style=\"background: #f0f0f0; color: blue\">where <\/span><span style=\"background: #f0f0f0\">c.City == <\/span><span style=\"background: #f0f0f0; color: #a31515\">\"London\"\n<\/span><span style=\"background: #f0f0f0; color: blue\">select <\/span><span style=\"background: #f0f0f0\">c.CompanyName).Sum();<\/span><\/pre>\n<p>In my case, it became:<\/p>\n<pre class=\"code\"><span style=\"background: #f0f0f0\">        <\/span><span style=\"background: #f0f0f0; color: blue\">int <\/span><span style=\"background: #f0f0f0\">result = (<\/span><span style=\"background: #f0f0f0; color: blue\">from <\/span><span style=\"background: #f0f0f0\">o <\/span><span style=\"background: #f0f0f0; color: blue\">in <\/span><span style=\"background: #f0f0f0\">objectList\n<\/span><span style=\"background: #f0f0f0; color: blue\">where <\/span><span style=\"background: #f0f0f0\">o.OptionGroupId == specificId\n<\/span><span style=\"background: #f0f0f0; color: blue\">select <\/span><span style=\"background: #f0f0f0\">o.Cost).Sum();<\/span><\/pre>\n<p>It\u2019s just so simple and easy. Hooray!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve just found out how to combine .Sum() with a where clause, and all it took was a further step into the murky world of Linq. I got my tip-off from a fellow BlogEngine.NET blog user Gary Kilminster &#8211; http:\/\/cunningplan.co.uk\/post\/Extension-Methods.aspx \u2013 although it doesn\u2019t look like his blog has been updated for a while. I &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.pagan-gerbil.net\/?p=63\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Linq&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[43,44,24],"series":[],"_links":{"self":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/posts\/63"}],"collection":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63"}],"version-history":[{"count":0,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=\/wp\/v2\/posts\/63\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.pagan-gerbil.net\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}