<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Parallelcoding.com &#187; LINQ</title>
	<atom:link href="http://www.parallelcoding.com/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parallelcoding.com</link>
	<description></description>
	<lastBuildDate>Mon, 16 Aug 2010 11:55:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Left Joins in LINQ</title>
		<link>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/</link>
		<comments>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 14:15:10 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=438</guid>
		<description><![CDATA[For some reason it seems that I often need to perform a left join in LINQ. Every time I need to do this I find myself scouring the web one more time in order to remember how a left join works in LINQ. So how does it work? The best example that I&#8217;ve found is [...]]]></description>
			<content:encoded><![CDATA[<p>For some reason it seems that I often need to perform a left join in LINQ. Every time I need to do this I find myself scouring the web one more time in order to remember how a left join works in LINQ. So how does it work? The best example that I&#8217;ve found is <a href="http://geekswithblogs.net/AzamSharp/archive/2008/04/07/121103.aspx">here</a>. The example looks something like this:</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var list <span style="color: #008000;">=</span> from r <span style="color: #0600FF;">in</span> dc.<span style="color: #0000FF;">tblRooms</span>
             join ui <span style="color: #0600FF;">in</span> dc.<span style="color: #0000FF;">tblUserInfos</span>
             on r.<span style="color: #0000FF;">UserName</span> equals ui.<span style="color: #0000FF;">UserName</span> into userrooms
             where r.<span style="color: #0000FF;">CourseID</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1848</span>
             from ur <span style="color: #0600FF;">in</span> userrooms.<span style="color: #0000FF;">DefaultIfEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
             select <span style="color: #008000;">new</span><span style="color: #000000;">&#123;</span>
                 FirstName <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ur.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;N/A&quot;</span> <span style="color: #008000;">:</span> ur.<span style="color: #0000FF;">FirstName</span>,
                 LastName <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ur.<span style="color: #0000FF;">LastName</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;N/A&quot;</span> <span style="color: #008000;">:</span> ur.<span style="color: #0000FF;">LastName</span>,
                 RoomName <span style="color: #008000;">=</span> r.<span style="color: #0000FF;">Name</span>
              <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>I like this example a lot because it is very straight forward. Personally I would like to make the statement a bit more generic. In order to do that all we need to remember is that every Left Join has a left table A and a right table B. All the results from the A will be returned regardless of the join with B. In my example I will call table A the LEFT_TABLE and table B the RIGHT_TABLE.</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var list <span style="color: #008000;">=</span>  from LT <span style="color: #0600FF;">in</span> LEFT_TABLE
	      join RT <span style="color: #0600FF;">in</span> RIGHT_TABLE
	      on LT.<span style="color: #0000FF;">key</span> equals RT.<span style="color: #0000FF;">KEY</span> into NEW_TABLE
              where <span style="color: #008000;">&lt;</span>CONDITIONS<span style="color: #008000;">&gt;</span>
              from NT <span style="color: #0600FF;">in</span> NEW_TABLE.<span style="color: #0000FF;">DefaultIfEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
              <span style="color: #008000;">&lt;</span>SELECT_STATEMENT<span style="color: #008000;">&gt;;</span></pre></td></tr></table></div>

<p>
The only problem that may occur with this left join occurs with the DefaultIfEmpty() operator. A better practice would be to pass in a default value so that we can know what to expect in return.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
