Assuming I have a left outer join as such:
from f in Foo
join b in Bar on f.Foo_Id equals b.Foo_Id into g
from result in g.DefaultIfEmpty()
select new { Foo = f, Bar = result }
How would I express the same task using extension methods? E.g.
Foo.GroupJoin(Bar, f => f.Foo_Id, b => b.Foo_Id, (f,b) => ???)
.Select(???)
Since this seems to be the de facto SO question for left outer joins using the method (extension) syntax, I thought I would add an alternative to the currently selected answer that (in my experience at least) has been more commonly what I'm after
To display the difference using a simple data set (assuming we're joining on the values themselves):
Option 2 is true to the typical left outer join definition, but as I mentioned earlier is often unnecessarily complex depending on the data set.
Improving on Ocelot20's answer, if you have a table you're left outer joining with where you just want 0 or 1 rows out of it, but it could have multiple, you need to Order your joined table:
Otherwise which row you get in the join is going to be random (or more specifically, whichever the db happens to find first).
Turning Marc Gravell's answer into an extension method, I made the following.
You can create extension method like:
Whilst the accepted answer works and is good for Linq to Objects it bugged me that the SQL query isn't just a straight Left Outer Join.
The following code relies on the LinkKit Project that allows you to pass expressions and invoke them to your query.
It can be used as follows