{"id":1291,"date":"2014-02-13T21:23:58","date_gmt":"2014-02-13T21:23:58","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1291"},"modified":"2014-02-13T21:23:58","modified_gmt":"2014-02-13T21:23:58","slug":"some-linq-patterns","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/some-linq-patterns\/","title":{"rendered":"Some Linq Patterns"},"content":{"rendered":"<p>What follows is a list of a few patterns for specific tasks using LINQ<\/p>\n<p><strong>Combining two lists<\/strong><\/p>\n<p>We can combine two lists, where duplicates are removed using<\/p>\n<p>var a = new List<int> {0, 2, 3, 4, 5, 6};<br \/>\nvar b = new List<int> { 0, 1, 3, 4, 6, 6 };<\/p>\n<p>var union = a.Union(b);<br \/>\n[\/code]<\/p>\n<p><strong>Find the common items in both lists<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar a = new List&lt;int&gt; {0, 2, 3, 4, 5, 6};\r\nvar b = new List&lt;int&gt; { 0, 1, 3, 4, 6, 6 };\r\n\r\nvar intersection = a.Intersect(b);\r\n<\/pre>\n<p>we can also use the lambda expression <em>join<\/em><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar l1 = new List&lt;int&gt; {1, 2, 4, 5};\r\nvar l2 = new List&lt;int&gt; { 0, 2, 4, 6 };\r\n\r\nvar r = from a in l1 join b in l2 on a equals b select a;\r\n<\/pre>\n<p><strong>Find the differences between two lists<\/strong><\/p>\n<p>This will find the items in the first list which are <strong>not<\/strong> in the second list<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar listA = new List&lt;int&gt; {0, 2, 3, 4, 5, 6};\r\nvar listB = new List&lt;int&gt; { 0, 1, 3, 4, 6, 6 };\r\n\r\nvar differences = from a in listA where listB.All(i =&gt; i != a) select a;\r\n<\/pre>\n<p><strong>Combining every item in one list with every item in another list<\/strong><\/p>\n<p>Here we&#8217;re going to do a cross join, where the output enumerable is a made up of each element from the first list, combined with each element of the second list<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar l1 = new List&lt;int&gt; {1, 2};\r\nvar l2 = new List&lt;int&gt; { 0, 2 };\r\n\r\nvar crossjoin = from a in l1\r\n       from b in l2\r\n       select new {a, b};\r\n<\/pre>\n<p>The output would be<\/p>\n<p>1 0<br \/>\n1 2<br \/>\n2 0<br \/>\n2 2<\/p>\n<p>I&#8217;ll add more as and when I remember to add them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What follows is a list of a few patterns for specific tasks using LINQ Combining two lists We can combine two lists, where duplicates are removed using var a = new List {0, 2, 3, 4, 5, 6}; var b = new List { 0, 1, 3, 4, 6, 6 }; var union = a.Union(b); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,3,64],"tags":[],"class_list":["post-1291","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-linq"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1291"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1291\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1291\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}