tags:

views:

50

answers:

2

Hi

Usually linq is using an left outer join for its queries but on some cases it decides to use inner join instead. I have a situation where that decision results in wrong results since the second table doesn't always have suitable records and that removes the records from the first table. I'm using a linqdatasource over a dbml where the relevant tables are identical but one holds historical records removed from the first. both have the same primary key. and I'm using a dataloadoption to load both tables at once with out round trips.

Would you explain why linq decided to use an inner join here?

Thanks

+1  A: 

No, unfortunately you are incorrect. The LINQ Join operator always does an inner join. http://www.hookedonlinq.com/JoinOperator.ashx

If you want to do a left outer join, you need to use a combination of select or foreach. See these examples:

or google it, there are a ton of examples out there.

Kirk Broadhurst
ps if you thoroughly learn the `Select` operator you can get away without using the `Join` operator.
Kirk Broadhurst
+1  A: 

It is better practice not to use the join operator where possible, and rely on traversing the relationships set up in the data context between the entities.

from r in Rabbits
select
{
    r.Name
    r.Parent.Name
}

This will automatically traverse the parent relationship and decide on the appropriate sql to run.

Using .DefaultIfEmpty() appropriately will mean that any NULL entries are added, hence it is translated into the appropriate outer join. This can be used with the join operator by including an 'into'.

http://stackoverflow.com/questions/1013031/join-and-left-join-equivalent-in-linq

SteadyEddi
I'm doning just that and using the dataloadoptions to get both Rabbit and Parent in the same query. My question is how does linq decide the type of join to use between rabits and parents ?
Thats decided by what you select from and whether the fields involved in the relationship are nullable.
SteadyEddi