{"id":88,"date":"2009-01-31T14:47:00","date_gmt":"2009-01-31T14:47:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2009\/01\/31\/join-and-group-tables\/"},"modified":"2009-01-31T14:47:00","modified_gmt":"2009-01-31T14:47:00","slug":"join-and-group-tables","status":"publish","type":"post","link":"http:\/\/www.panahy.nl\/index.php\/2009\/01\/31\/join-and-group-tables\/","title":{"rendered":"Join And Group Tables"},"content":{"rendered":"<p>This article is based on the great book of <span style=\"font-weight: bold;\">LINQ in Action<\/span>. I am learning so much from it and I would like to keep a note of some handy subject from the book while not disturbing any copyright of ther authors. For the full story please buy the book from\u00a0http:\/\/www.manning.com\/LINQinAction<\/p>\n<div>\n<p><span style=\"font-weight:bold;\">Group Join<\/span><\/p>\n<pre><br \/>from publisher in SampleData.Publishers<br \/>join book in SampleData.Books<br \/>  on publisher equals book.Publisher into publisherBooks<br \/>select new { Publisher=publisher.Name, Books=publisherBooks };<br \/><\/pre>\n<p>This is a <i>group join<\/i>. It bundles each publisher&#8217;s book as sequences named publisherBooks. This new query is equivalent to this one: <\/p>\n<pre><br \/>from book in SampleData.Books <br \/>group book by book.Publisher into publisherBooks<br \/>select new { Publisher=publisherBooks.Key.Name, Books=publisherBooks };<br \/><\/pre>\n<p><span style=\"font-weight:bold;\">Inner Join<\/span><br \/>An inner join essentially finds the intersection between two sequences. With an inner join, the elements from two sequences that meet a matching condition are combined to from a single sequence.<\/p>\n<pre><br \/>from publisher in SampleData.Publishers<br \/>join book in SampleData.Books<br \/>  on publisher equals book.Publisher<br \/>select new { Publisher=publisher.Name, Book=book.Title };<br \/><\/pre>\n<p><span style=\"font-weight:bold;\">Left Outer Join<\/span><br \/>When we want to keep all the elements from the outer sequence, independently of whether there is a matching element in the inner sequence, we need to perform a <i>left outer join<\/i>.<\/p>\n<p>A left outer join is like an inner inner join, except that all the left-side elements get included at least once, even if they don&#8217;t match any right side elements. <\/p>\n<pre><br \/>from publisher in SampleData.Publishers<br \/>join book in SampleData.Books<br \/>  on publisher equals book.Publisher into publisherBooks<br \/>from book in publisherBooks.DefaultIfEmpty()<br \/>select new { <br \/>  Publisher = publisher.Name, <br \/>  Book = book == default(Book) ? \"(no books)\" : book.Title<br \/>};<br \/><\/pre>\n<p><code>DefaultEmpty<\/code> operator supplies a default element for an empty sequence. <br \/><code>DefaultEmpty<\/code> uses the <code>default<\/code> keyword of generics. It returns null for reference types and zero for numeric value types. For structs, it returns each member of the struct initialized to zero or null depending on whether they are value or reference types.<\/p>\n<p><span style=\"font-weight:bold;\">Cross Join<\/span><br \/>A <i>cross join<\/i> computes the Cartesian product of all the elements from two sequences. The result is a sequence that contains a combination of each element from the first sequence with eacht element from the second sequence.<\/p>\n<pre><br \/>from publisher in SampleData.Publishers<br \/>from book in SampleData.Books<br \/>select new { <br \/>  Correct = (publisher == book.Publisher)<br \/>  Publisher = publisher.Name, <br \/>  Book = book.Title<br \/>};<br \/><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article is based on the great book of LINQ in Action. I am learning so much from it and I would like to keep a note of some handy subject from the book while not disturbing any copyright of ther authors. For the full story please buy the book from\u00a0http:\/\/www.manning.com\/LINQinAction Group Join from publisher &hellip; <a href=\"http:\/\/www.panahy.nl\/index.php\/2009\/01\/31\/join-and-group-tables\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Join And Group Tables&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/www.panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"This article is based on the great book of LINQ in Action. I am learning so much from it and I would like to keep a note of some handy subject from the book while not disturbing any copyright of ther authors. For the full story please buy the book from\u00a0http:\/\/www.manning.com\/LINQinAction Group Join from publisher&hellip;","_links":{"self":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/88"}],"collection":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":0,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}