<?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>Eric Fickes &#187; linq to sql</title>
	<atom:link href="http://ericfickes.com/tag/linq-to-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://ericfickes.com</link>
	<description>Design minded Internet Programmer</description>
	<lastBuildDate>Fri, 28 Oct 2011 04:14:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to JOIN two tables using LINQ to SQL</title>
		<link>http://ericfickes.com/2010/02/how-to-join-two-tables-using-linq-to-sql/</link>
		<comments>http://ericfickes.com/2010/02/how-to-join-two-tables-using-linq-to-sql/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 01:27:53 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[linq to sql]]></category>
		<category><![CDATA[linqtosql]]></category>
		<category><![CDATA[query]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1065</guid>
		<description><![CDATA[Wanted to share this since it gave me so much trouble figuring out.  It&#8217;s a simple SQL query ported to LINQ to SQL that joins two tables to return a filtered listed of data. Here are the tables from my &#8230; <a href="http://ericfickes.com/2010/02/how-to-join-two-tables-using-linq-to-sql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Wanted to share this since it gave me so much trouble figuring out.  It&#8217;s a simple SQL query ported to LINQ to SQL that joins two tables to return a filtered listed of data.</p>
<p>Here are the tables from my schema<br />
<a href="http://ericfickes.com/wp-content/uploads/2010/02/user-user_videos-video.png" rel="lightbox[1065]"><img class="aligncenter size-full wp-image-1068" title="My three user tables" src="http://ericfickes.com/wp-content/uploads/2010/02/user-user_videos-video.png" alt="user, user_video, video tables" width="465" height="317" /></a></p>
<p>Here is a basic SQL statement I could fire to retrieve my user videos.</p>
<pre class="brush: sql; title: ; notranslate">
select  *
from    video v, user_videos uv
where   v.vid = uv.vid
and     uv.uid = 2
</pre>
<p><a href="http://ericfickes.com/wp-content/uploads/2010/02/uservideos-SQL.png" rel="lightbox[1065]"><img class="aligncenter size-full wp-image-1069" title="Data returned from this SQL statement" src="http://ericfickes.com/wp-content/uploads/2010/02/uservideos-SQL.png" alt="User 2 has two videos" width="499" height="220" /></a><br />
Here is how you would run the same query using .net&#8217;s LINQ to SQL.</p>
<pre class="brush: csharp; title: ; notranslate">
// create DB connection
var db = new DBCONN();
// run query
List&lt;video&gt; uvids = (
    from c in db.video
    join o in db.user_videos
    on c.vid equals o.vid
    where o.uid == 2
    select c
).ToList();
</pre>
<p>This query differs slightly from the screenshot below because I used it in a WCF Service.</p>
<p><a href="http://ericfickes.com/wp-content/uploads/2010/02/uservideos-LINQTOSQL.png" rel="lightbox[1065]"><img class="aligncenter size-full wp-image-1070" title="Same query run via LINQ to SQL" src="http://ericfickes.com/wp-content/uploads/2010/02/uservideos-LINQTOSQL.png" alt="Same data, different retrieval method" width="488" height="419" /></a></p>
<p>The variable DBCONN is my database connection that I established when mapping my DB.  If you are not familiar with how to set this up, use the Visual Studio&#8217;s &#8220;Add the ADO.NET Entity Data Model&#8221; wizard.  With your .net project open, right click your project, left click on &#8220;Add the ADO.NET Entity Data Model&#8221;.  This wizard will walk you through setting up everything you need to setup your DB model file ( edmx ), as well as setting up your database connection and saving it in web.config.</p>
<p>Jesse Liberty did a <a title="ADO.NET DataEntities and WCF Feeding a Silverlight DataGrid" href="http://silverlight.net/learn/tutorials/adonetdataentities-cs/" target="_blank">simple tutorial that uses this wizard in a WCF service application</a>.</p>
<p>I hope this helps somebody out.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/02/how-to-join-two-tables-using-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

