{"id":6153,"date":"2012-11-07T07:00:00","date_gmt":"2012-11-07T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/11\/07\/instead-of-trying-to-create-a-filter-that-includes-everything-try-just-omitting-the-filter\/"},"modified":"2012-11-07T07:00:00","modified_gmt":"2012-11-07T07:00:00","slug":"instead-of-trying-to-create-a-filter-that-includes-everything-try-just-omitting-the-filter","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20121107-00\/?p=6153","title":{"rendered":"Instead of trying to create a filter that includes everything, try just omitting the filter"},"content":{"rendered":"<p>\nThe question was sent to a peer-to-peer discussion group for\nan internal program,\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/04\/16\/9551818.aspx\">\nlet&#8217;s call it Program Q<\/a>.\nI&#8217;ll add additional context so you can follow along.\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nHi, I&#8217;m trying to build a query that finds all issues owned by\na particular user, regardless of which product the issue belongs to.\nI know that I can query for specific products by saying\n<\/p>\n<pre>\nq select -owner bob -product LitWare\nq select -owner bob -product Contoso\n<\/pre>\n<p>\nIs there a better way to do this than just running the query\nfor every product in the database?\nIt would be great to find all the issues at one shot\ninstead of having to issue dozens of commands and combine the results.\n<\/p>\n<\/blockquote>\n<p>\nThe person who submitted this question got so distracted by the\n<code>-product<\/code> filter, that they forgot that they could\njust omit the filter.\n<\/p>\n<pre>\nq select -owner bob\n<\/pre>\n<p>\nIf you don&#8217;t filter by product, then it finds everything\nregardless of the product.\n<\/p>\n<p>\nEnumerating all the products, then repeating the query for\neach product is some sort of anti-pattern.\nI don&#8217;t know if it has a name, so I&#8217;ll make one up.\nThe <i>long division<\/i> anti-pattern\nperforms an operation on a collection by arbitrarily breaking\nthe collection into groups,\nthen performing the operation on each member of each group,\nall this even though the grouping is unrelated to the operation.\n<\/p>\n<p>\nIn C#, it would be phrased something like this:\n<\/p>\n<pre>\npublic void MakeUnavailable(string productId)\n{\n    var stores = Inventory.Select(p =&gt; p.Store).Distinct();\n    foreach (var store in stores) {\n        foreach (var product in\n                 from p in Inventory\n                 where p.Store == store &amp;&amp;\n                       p.ProductId == productId) {\n            product.Available = false;\n        }\n    }\n}\n<\/pre>\n<p>\nIn words, we first dig through the inventory and collect\nall the unique stores.\nFor each store, we go through the inventory again,\nlooking for products from that store,\nand if the product is the one we want to make unavailable,\nset the <code>Available<\/code> property to <code>false<\/code>.\n(To avoid playing favorites,\nI used both fluent and query expression syntax.)\n<\/p>\n<p>\nAssuming the order in which the product is made unavailable\nis not important\n(and it doesn&#8217;t appear to be, since we didn&#8217;t sort the stores),\nthe grouping by store is superfluous.\nYou can just iterate across the entire inventory without regard\nfor store:\n<\/p>\n<pre>\npublic void MakeUnavailable(string productId)\n{\n    foreach (var product in\n             from p in Inventory\n             where p.ProductId == productId\n             select p) {\n        product.Available = false;\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The question was sent to a peer-to-peer discussion group for an internal program, let&#8217;s call it Program Q. I&#8217;ll add additional context so you can follow along. Hi, I&#8217;m trying to build a query that finds all issues owned by a particular user, regardless of which product the issue belongs to. I know that I [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-6153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>The question was sent to a peer-to-peer discussion group for an internal program, let&#8217;s call it Program Q. I&#8217;ll add additional context so you can follow along. Hi, I&#8217;m trying to build a query that finds all issues owned by a particular user, regardless of which product the issue belongs to. I know that I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=6153"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6153\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=6153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=6153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=6153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}