IQueryable left outer join linq extension I wanted to create an extension method to enable left outer joins in linq/lambda IQueryables, and I have come up with the following, but I am still not sure it's completely correct. 

public static IQueryable&lt;TResult&gt; LeftOuterJoin&lt;TOuter, TInner, TKey, TResult&gt;(
    this IQueryable&lt;TOuter&gt; outer,
    IQueryable&lt;TInner&gt; inner,
    Expression&lt;Func&lt;TOuter, TKey&gt;&gt; outerKeySelector,
    Expression&lt;Func&lt;TInner, TKey&gt;&gt; innerKeySelector,
    Func&lt;TOuter, TInner, TResult&gt; resultSelector)
{
    return outer.GroupJoin(
        inner,
        outerKeySelector,
        innerKeySelector,
        (o, i) =&gt; new { outer = o, innerCollection = i })
    .SelectMany(
        a =&gt; a.innerCollection.DefaultIfEmpty(),
        (a, i)=&gt;resultSelector(a.outer,i));
}


I am especially concerned with the resultSelector being a Func, not an Expression, but I cant call an Expression in the lambda on the last line. I assume it gets cast automatically, but it's only an assumption.

Is this correct? How can I improve this? Can it be optimized more?
Actually, it is way more complex than that, since Funcs&lt;> cant't be translated to the database, so you'll have to "call" the expression. But since you can't call a expression, you'll have to change the expression into another that means what you need it to means. 

Check out this question on StackOverflow. It has an good explanation on Linq inner workings and the accepted answer has a method that do the outer join. Be sure to read the second answer too, since it shows some issues that arise on more complex queries. And just as a warning, the method you are trying to write will easily gets 150 lines long (that's why I didn't copied it here)...