﻿<?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>Programming By A Tool &#187; Join</title>
	<atom:link href="http://byatool.com/tag/join/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Linq Join Extension Method and How to Use It&#8230;</title>
		<link>http://byatool.com/c/linq-join-method-and-how-to-use-it/</link>
		<comments>http://byatool.com/c/linq-join-method-and-how-to-use-it/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 17:23:31 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Join]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=607</guid>
		<description><![CDATA[I don&#8217;t like using the query syntax when it comes to Linq to AnythingButTheKitchenSink . Not sure why. Mostly, I guess, is that I seem to have a liking for Funcs and Actions to the point of stupidity and although you can work them into the query syntax, it just doesn&#8217;t look right. Now with [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t like using the query syntax when it comes to Linq to AnythingButTheKitchenSink . Not sure why. Mostly, I guess, is that I seem to have a liking for Funcs and Actions to the point of stupidity and although you can work them into the query syntax, it just doesn&#8217;t look right.</p>
<p>Now with most of the Linq methods like Where or First, it&#8217;s simple once you understand lamdba expressions:</p>
<pre>.SomeMethod(someField =&gt; someField.Property == value);</pre>
<p>Now what about join?</p>
<pre><img src="http://byatool.com/wp-content/uploads/2009/04/join.png" alt="JOINHELL" /></pre>
<p>So inner selector with an outer selector and a selector selects a selecting selector. Right got it.</p>
<p>Well let&#8217;s try to break it down. First part is</p>
<pre><span style="color: #0000ff;">this</span> <span style="color: #008080;">IEnumerable</span>&lt;<span style="color: #008080;">TOuter</span>&gt;</pre>
<p>So being that this is an extension method meaning this is the collection you are using this method on.</p>
<pre><span style="color: #008080;">IEnumerable</span>&lt;<span style="color: #008080;">TInner</span>&gt; inner</pre>
<p>So second field must be the list you want to join to. Ok so far.</p>
<pre><span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">TOuter</span>, <span style="color: #008080;">TKey</span>&gt; outerKeySelector</pre>
<p>Now this is where it gets a little odd looking. We know we have Outer and Inner lists so there needs to be a way to join on something. Say Outer is User and Inner is UserAddress. Most likely you will have a UserID on both lists. If not, you do now. So basically what this part of the method is saying is &#8220;Give me the stupid key on the Outer (User) list that I should care about.&#8221;</p>
<pre>, user =&gt; user.UserID,</pre>
<p>Next part:</p>
<pre><span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">TInner</span>, <span style="color: #008080;">TKey</span>&gt; innerKeySelector</pre>
<p>Pretty much the same thing, except now it needs the key from the Innerlist (UserAddress):</p>
<pre>, address =&gt; address.UserID,</pre>
<p>Now for the fun part:</p>
<pre><span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">TOuter</span>, <span style="color: #008080;">TInner</span>, <span style="color: #008080;">TResult</span>&gt; resultSelector</pre>
<p>Sa&#8230;say what? Ok this may look weird at first but you&#8217;ll hate yourself for not seeing it. It&#8217;s just asking you what to select from the two lists as some kind of hybrid object. See, you have to remember that with these linq methods, each method will produce a list. You can&#8217;t just chain them together and have it remember every list you&#8217;ve made:</p>
<pre>   user.Where(user =&gt; user.UserID &gt; 1) <span style="color: #008000;">// gives me a list of users</span>
         .Select(user =&gt; new { user.UserName, user.UserAddress, user.UserID } <span style="color: #008000;">
         //Gives me new items with user name, address, and user id</span></pre>
<p>From this simple method chain, the end list is NOT the same as the one you started with or the one produced by the where method.</p>
<p>The last part of the Join method needs you to tell it what it&#8217;s going to produce from this join. Now it probably could just guess and include both lists, but that could be seen as sloppy and ultimately this gives you the choice of what exactly needs to be taken after the join. So:</p>
<pre>, (user, address) =&gt; new { user, address});</pre>
<p>So in this case, the newly created and joined list with be a list of items that have a user and address attached to it much like if you had a list of:</p>
<pre><span style="color: #0000ff;">class</span> UserAddressHybrid()
{
    <span style="color: #0000ff;">public</span> <span style="color: #008080;">User</span> user { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
    <span style="color: #0000ff;">public</span> <span style="color: #008080;">UserAddress</span> userAddress { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
}</pre>
<p>So in other words, WHAT DO YOU WANT YOUR RESULTS TO LOOK LIKE?</p>
<p>In full it would look something like:</p>
<pre>user.Join(address =&gt; address.User.UserID,  <span style="color: #008000;">//IEnumerable&lt;TInner&gt; inner</span>
             user =&gt; user.UserID,  <span style="color: #008000;">//Func&lt;TOuter, TKey&gt; outerKeySelector</span>
             address =&gt; address.UserID,  <span style="color: #008000;">//Func&lt;TInner, TKey&gt; innerKeySelector</span>
             (user, address) =&gt; new { user, address});  <span style="color: #008000;">//Func&lt;TOuter, TInner, TResult&gt; resultSelector</span></pre>
<p>Not so hard anymore, is it?  You can start kicking yourself now.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/c/linq-join-method-and-how-to-use-it/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>NHibernate Cross Join: The multi-part identifier &#8230; could not be bound..</title>
		<link>http://byatool.com/nhibernate/nhibernate-cross-join-the-multi-part-identifier-could-not-be-bound/</link>
		<comments>http://byatool.com/nhibernate/nhibernate-cross-join-the-multi-part-identifier-could-not-be-bound/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 18:37:40 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Hql]]></category>
		<category><![CDATA[Join]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=593</guid>
		<description><![CDATA[Something else I found out about NHibernate, at least an older version, and cross joins&#8230; and if you&#8217;re reading this chances are you have the same version. Say you want to cross join something but there are also other joins. You would think it should be like this: SELECT user.UserName, status.StatusName FROM User user, UserStatus [...]]]></description>
			<content:encoded><![CDATA[<p>Something else I found out about NHibernate, at least an older version, and cross joins&#8230; and if you&#8217;re reading this chances are you have the same version. Say you want to cross join something but there are also other joins. You would think it should be like this:</p>
<pre>    <span style="color: #800000;">SELECT
       user.UserName, status.StatusName
    FROM
       User user, UserStatus status
    LEFT JOIN
      user.Address address
    WHERE
      user.CreatedDate = status.CreatedDate</span></pre>
<p>Ok so once again, this query is kind of stupid but the idea is there. With NHibernate you have to Cross Join when you need to match two objects but not on the mapped keys. Meaning that User and Status might be mapped on UserID ie Status.UserID = User.UserID. But what if you wanted to match them on something else, like a date. Well that&#8217;s where the cross join came in. The Sql might look like:</p>
<pre>    <span style="color: #0000ff;">SELECT</span>
       user0_.UserName, status0_.StatusName
    <span style="color: #0000ff;">FROM</span>
       ByATool.User user0_, ByATool.UserStatus status0_
    <span style="color: #0000ff;">LEFT JOIN</span>
       ByATool.Address address0_ <span style="color: #0000ff;">ON</span> address0_.UserID = user0_.UserID
    <span style="color: #0000ff;">WHERE</span>
       user0_.CreatedDate = status0_CreatedDate</pre>
<p>Looks fine right? Well it&#8217;s not according to <span class="showItLink" xmlns:comment="And possibly higher.">SqlServer 2005</span>.  Turns out the cross join has to appear AFTER the other joins. So in reality it should look like:</p>
<pre>    <span style="color: #0000ff;">SELECT</span>
       user0_.UserName
    <span style="color: #0000ff;">FROM</span>
       ByATool.User user0_, ByATool.UserStatus status0_
    <span style="color: #0000ff;">LEFT JOIN</span>
       ByATool.Address address0_ ON address0_.UserID = user0_.UserID,
       status0_.StatusName
    <span style="color: #0000ff;">WHERE</span>
       user0_.CreatedDate = status0_CreatedDate</pre>
<p>Which means the hql looks like this:</p>
<pre>    <span style="color: #800000;">SELECT
       user.UserName
    FROM
       User user, UserStatus status
    LEFT JOIN
      user.Address address,
      status.StatusName
   WHERE
      user.CreatedDate = status.CreatedDate
</span></pre>
<p>See how the cross joined object is now after the Left Join? Looks odd but this is what it takes to get the cross join through. Kind of annoying.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/nhibernate/nhibernate-cross-join-the-multi-part-identifier-could-not-be-bound/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate.QueryException: expecting &#8216;elements&#8217; or &#8216;indices&#8217; after&#8230;</title>
		<link>http://byatool.com/nhibernate/nhibernatequeryexception-expecting-elements-or-indices-after/</link>
		<comments>http://byatool.com/nhibernate/nhibernatequeryexception-expecting-elements-or-indices-after/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 18:16:47 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Hql]]></category>
		<category><![CDATA[Join]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=590</guid>
		<description><![CDATA[Yes, it&#8217;s true. I have two lovers. Well more like I&#8217;m stuck with NHibernate due to some kind of stupid forced marriage and the other is my somewhat quirky but ultimately young and hot mistress Entity Framework. And now that I&#8217;ve admitted to such an atrocity you are about to run screaming, I&#8217;ll get on [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, it&#8217;s true. I have two lovers. Well more like I&#8217;m stuck with NHibernate due to some kind of stupid forced marriage and the other is my somewhat quirky but ultimately young and hot mistress Entity Framework. And now that I&#8217;ve admitted to such an atrocity you are about to run screaming, I&#8217;ll get on with what the hell this means.</p>
<p>So I ran across this the other day and well I&#8217;m sure a smart person would immediately understand what it means, but that smart person doesn&#8217;t write for this blog. You&#8217;re stuck with me. So after some staring at the screen and praying to whatever god I just learned about on wikipedia, I figured it out. Now I can&#8217;t promise this is the only reason to get this error, but this is the answer I have.</p>
<p>Say you have a user and with that user you have a list of events. Now it should be obvious that there is an event class, an event table, and user table. Simple enough. Now in hql, it would look something like this:</p>
<pre>    <span style="color: #800000;">SELECT
       user
    FROM
       User user
    LEFT JOIN
       user.Events event
    WHERE
       user.Events.Status = SomeStatusEnum.SomeValue
</span></pre>
<p>And yes I realize that it doesn&#8217;t need to be a left join, but just roll with me on this because I don&#8217;t feel like making a better example. So if you look close enough at the query you should see something wrong&#8230; Why would I join the objects and then refer to the event status through the user?</p>
<pre>       <span style="color: #800000;">user.Events.Status = SomeStatusEnum.SomeValue</span></pre>
<p>Because I&#8217;m a tool. Yeah turns out that, to me, crytic message is really saying &#8220;Use the joined object alias you f-ing tool.&#8221; In other words:</p>
<pre>       <span style="color: #800000;">events.Status = SomeStatusEnum.SomeValue</span></pre>
<p>And boom. No more error.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/nhibernate/nhibernatequeryexception-expecting-elements-or-indices-after/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Join By Anonymous Types in Linq</title>
		<link>http://byatool.com/general-coding/joining-by-anonymous-types/</link>
		<comments>http://byatool.com/general-coding/joining-by-anonymous-types/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 13:20:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[Anonymous Types]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Join]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/09/joining-by-anonymous-types/</guid>
		<description><![CDATA[Just found this out yesterday so I thought I would post and pass on to all two of you reading this. Suppose you have a User table and a Contacts table and you wanted to find all the users that match up with the contacts table. Now suppose there is no direct correlation. What to [...]]]></description>
			<content:encoded><![CDATA[<p>Just found this out yesterday so I thought I would post and pass on to all two of you reading this.</p>
<p>Suppose you have a User table and a Contacts table and you wanted to find all the users that match up with the contacts table. Now suppose there is no direct correlation. What to do? You could do something really brilliant by joining the tables together on FirstName and LastName, because we all know that there will always only be one John Smith in either table. Screw you, I couldn&#8217;t think of a better example at the time.</p>
<pre><span style="color: #3333ff;">public static</span> <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">User</span>&gt; GetAllUsersWithMatchingContactInformationUsingJoin()
{
  <span style="color: #3333ff;">List</span>&lt;<span style="color: #00cccc;">User</span>&gt; foundUsers;

  <span style="color: #3333ff;">var</span> query = <span style="color: #3333ff;">from</span> user <span style="color: #3333ff;">in</span> dataContext.Users
              <span style="color: #3333ff;">join </span>contact in dataContext.Contacts <span style="color: #3333ff;">on new</span> {user.FirstName, user.LastName } <span style="color: #3333ff;">equals new</span> { contact.FirstName, contact.LastName }
              select user;

              foundUsers = query.ToList();

  <span style="color: #3333ff;">return</span> foundUsers;
}</pre>
<p>As you can see here:</p>
<pre><span style="color: #3333ff;">join </span>contact <span style="color: #3333ff;">in</span> dataContext.Contacts <span style="color: #3333ff;">on new</span> {user.FirstName, user.LastName } <span style="color: #3333ff;">equals new</span> { contact.FirstName, contact.LastName }</pre>
<p>You can create a type on the fly and then compare it to another. I thought that was interesting.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/joining-by-anonymous-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

