{"id":59,"date":"2008-09-08T20:11:38","date_gmt":"2008-09-09T00:11:38","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=59"},"modified":"2024-03-01T17:59:36","modified_gmt":"2024-03-01T22:59:36","slug":"enumerableintersect-enumerableexcept-and-enumerableunion","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2008\/09\/08\/enumerableintersect-enumerableexcept-and-enumerableunion\/","title":{"rendered":"Enumerable.Intersect, Enumerable.Except, and Enumerable.Union"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/Sept2008\/ProLinqApress.gif\" alt=\"Pro LINQ: Language Integrated Query in C# 2008\" title=\"Pro LINQ: Language Integrated Query in C# 2008\" style=\"float:left; padding: .5em;\" \/>I love when things come up just in time for me to need them for a project that I&#8217;m involved in.  Currently, I need to take a bunch of results and find only the intersection of those results.  I was contemplating doing some lambdas to compare the lists, but then I was reading <a href=\"http:\/\/www.apress.com\/book\/view\/1590597893\">Pro LINQ: Language Integrated Query in C# 2008<\/a> and I found the Intersect() method.  (Note: Thanks to <a href=\"https:\/\/twitter.com\/jeffreymeyer\">Jeff Meyer<\/a> for loaning me the book in such a timely fashion.)<\/p>\n<p>The code looks like this:<\/p>\n<pre>\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\n\r\nnamespace Linq\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            var listOne = new List&lt;int&gt;() { 1, 2, 3, 4, 5};\r\n            var listTwo = new List&lt;int&gt;() { 3, 4, 5, 6, 7};\r\n\r\n            var intIntersect = listOne.Intersect(listTwo);\r\n\r\n            foreach (var i in intIntersect)\r\n            {\r\n                Console.WriteLine(i);\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>What is output is <\/p>\n<pre>\r\n3\r\n4\r\n5\r\n<\/pre>\n<p>There are two important things to note.  First of all, you can use any IEnumerable to perform an Intersect.  Secondly, it is important to realize that you are comparing from the first list to the second list.  This isn&#8217;t important when doing an Intersect(), but lets look at another example.<\/p>\n<pre>\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\n\r\nnamespace Linq\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            var listThree = new string[] { \"Pete\", \"On\", \"Software\" };\r\n            var listFour = new string[] { \"Joel\", \"On\", \"Software\" };\r\n\r\n            var stringExcept = listThree.Except(listFour);\r\n\r\n            foreach (var s in stringExcept)\r\n            {\r\n                Console.WriteLine(s);\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>The output of this code is <\/p>\n<pre>\r\nPete\r\n<\/pre>\n<p>In this example, I used a string array instead of a generic List to show that other IEnumerables could be used.  When calling the Except() method, I get the unique value(s) from the first IEnumerable.  Intersect() would have returned <\/p>\n<pre>\r\nOn\r\nSoftware\r\n<\/pre>\n<p>and if I had written <\/p>\n<pre>\r\nvar stringExcept = listFour.Except(listThree);\r\n<\/pre>\n<p>it would have returned <\/p>\n<pre>\r\nJoel\r\n<\/pre>\n<p>so much more care is needed when using Except() to ensure exactly which group has the unique values that you want to keep.  However, there is one more thing you can do.  What if you want to find every distinct value between the two lists?  You would do something like the following<\/p>\n<pre>\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\n\r\nnamespace Linq\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            var listThree = new string[] { \"Pete\", \"Pete\", \"On\", \"Software\" };\r\n            var listFour = new string[] { \"Joel\", \"On\", \"Software\", \"Software\" };\r\n\r\n            var uniqueStrings = listFour.Union(listThree);\r\n\r\n            foreach (var s in uniqueStrings)\r\n            {\r\n                Console.WriteLine(s);\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>which returns<\/p>\n<pre>\r\nJoel\r\nOn\r\nSoftware\r\nPete\r\n<\/pre>\n<p>The above shows all of the unique values from the first group and any of the unique values that the second group brings to the party that the first group didn&#8217;t already have.  <\/p>\n<p>This is pretty powerful stuff if you have to process lists and should ensure that you are doing the most efficient operations possible.  Linq is, of course, pretty exciting stuff and as I uncover more nuggets from the <a href=\"http:\/\/www.apress.com\/book\/view\/1590597893\">Pro LINQ: Language Integrated Query in C# 2008<\/a> book, I will share them here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love when things come up just in time for me to need them for a project that I&#8217;m involved in. Currently, I need to take a bunch of results and find only the intersection &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,35],"tags":[89,101],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-code-tips","category-linq","tag-code-tips","tag-linq"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}