# 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<TResult> LeftOuterJoin<TOuter, TInner, TKey, TResult>(
this IQueryable<TOuter> outer,
IQueryable<TInner> inner,
Expression<Func<TOuter, TKey>> outerKeySelector,
Expression<Func<TInner, TKey>> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector)
{
return outer.GroupJoin(
inner,
outerKeySelector,
innerKeySelector,
(o, i) => new { outer = o, innerCollection = i })
.SelectMany(
a => a.innerCollection.DefaultIfEmpty(),
(a, i)=>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?

• Have tested it? Does it generate a left outer join sql? – t3chb0t Jan 13 '17 at 13:33
• I've tested it on AsQueriable collections, unfortunately haven't yet had the opportunity to test it on an actual database – Kravaros Jan 17 '17 at 12:16
• If you didn't actually test it, how would you know if the improvements are improvements at all? – t3chb0t Jan 17 '17 at 12:25