Posted By: JoshRoss | Nov 7th @ 6:16 PM
page 1 of 1
Comments: 9 | Views: 335
JoshRoss
JoshRoss
A righteous infliction of retribution manifested by an appropriate agent.

Does anyone know why outer joins in linq are so crazy?


Example:
 

Dim z = From c In db.Customers
Group Join o In db.Orders
On c.CustID Equals o.CustID
Into j
From jo In j.DefaultIfEmpty()
Select New With {c, jo}


There has to be a good story behind this.

 

figuerres
figuerres
???

what about this is "so crazy" ?

 

 

If you're going to do left outer join, you should do right outer join and full outer join, too.

 

The syntax should translate to API calls so, as an exercise, write the API that a left join would become after translation and then, once you've done that, map your syntax to that API in a way that a compiler could implement.

 

I would definitely post your suggestion in the "Ideas for C# 5.0" thread in TechOff if you haven't already.

I'd imagine it's because SQL outer join semantics don't make much sense, and they figured that when you use them you're probably really trying to simulate a group join, so in a language with composite object types why not just support that directly instead?

I don't really have time to give you a full answer (or draw a picture -- sorry), but basically, when you do a left outer join in SQL between two tables A and B, what you really want in return is something that lets you answer the question "for each element (or for a given element) in A, what is the set of elements of B associated with it (using such and such criteria for association) ?", right?  And you want to be able to get an answer even if that answer is "the empty set".  But since a SQL query must return a table, and since (IIRC) individual cells in standard SQL can't have set or collection types, outer joins awkwardly simulate this by doing an inner join but adding extra rows, with null values* for all the columns of B, wherever there's an A with no associated Bs.  This is usable, but doesn't really make any logical sense -- a set of Bs with no members is not the same as a B whose columns are all empty (or unknown)!  Since .NET has its object system with collection types, etc., it doesn't have to do this -- it can just take each A, and directly associate it with a collection of corresponding Bs, which may be empty if no such Bs exist.  

 

* (which in SQL are problematic in themselves, since SQL can't make up its mind whether they mean "there is nothing here" or "there is something here but I don't know what it is")

I do outer joins when I want to get child rows from an "optional" table, so returning a row of all nulls makes perfect sense to me.

page 1 of 1
Comments: 9 | Views: 335
Microsoft Communities