Skip to content
Advertisement

LINQ to SQL – Left Outer Join with multiple join conditions

I have the following SQL, which I am trying to translate to LINQ:

I have seen the typical implementation of the left outer join (ie. into x from y in x.DefaultIfEmpty() etc.) but am unsure how to introduce the other join condition (AND f.otherid = 17)

EDIT

Why is the AND f.otherid = 17 condition part of the JOIN instead of in the WHERE clause? Because f may not exist for some rows and I still want these rows to be included. If the condition is applied in the WHERE clause, after the JOIN – then I don’t get the behaviour I want.

Unfortunately this:

seems to be equivalent to this:

which is not quite what I’m after.

Advertisement

Answer

You need to introduce your join condition before calling DefaultIfEmpty(). I would just use extension method syntax:

Or you could use a subquery:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement