{"id":109451,"date":"2024-02-26T07:00:00","date_gmt":"2024-02-26T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109451"},"modified":"2024-02-26T10:16:06","modified_gmt":"2024-02-26T18:16:06","slug":"20240226-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240226-00\/?p=109451","title":{"rendered":"A C# LINQ one-liner to check if exactly one of a set of conditions is met"},"content":{"rendered":"<p>I was writing a tool that needed to check whether exactly one of a list of conditions was true. One way of doing this is to count them manually:<\/p>\n<pre>bool DoesExactlyOneConditionApply(DataSet data)\r\n{\r\n    int count = 0;\r\n    if (data.Widgets.Count == 1)\r\n    {\r\n        ++count;\r\n    }\r\n    if (data.Gadgets.Count == 1)\r\n    {\r\n        ++count;\r\n    }\r\n    if (data.Doodads.Count &gt; data.Sockets.Count)\r\n    {\r\n        ++count;\r\n    }\r\n    if (data.Approvers.Contains(Approver.Director))\r\n    {\r\n        ++count;\r\n    }\r\n    return count == 1;\r\n}\r\n<\/pre>\n<p>This works and is efficient, but is rather wordy and susceptible to copy\/pasta bugs.<\/p>\n<p>Performance was not an issue in this code because most of the time was being spent in calculating the DataSet, not in checking whether conditions apply.<\/p>\n<p>Here&#8217;s what I came up with:<\/p>\n<pre>bool DoesExactlyOneConditionApply(DataSet data)\r\n{\r\n    return new[] {\r\n        data.Widgets.Count == 1,\r\n        data.Gadgets.Count == 1,\r\n        data.Doodads.Count &gt; data.Sockets.Count,\r\n        data.Approvers.Contains(Approver.Director),\r\n    }.Count(v =&gt; v) == 1;\r\n}\r\n<\/pre>\n<p>This builds a temporary array that holds the results of each test, and we ask LINQ to tell us how many of them are <code>true<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Maybe not the most efficient, but it&#8217;s easy to write.<\/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":[25],"class_list":["post-109451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Maybe not the most efficient, but it&#8217;s easy to write.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109451","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=109451"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109451\/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=109451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}