Created
April 9, 2015 19:03
-
-
Save jeremybeavon/807b4b9fa012ba5e7b7d to your computer and use it in GitHub Desktop.
LINQ Full Join
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IEnumerable<TResult> FullJoin<TOuter, TInner, TKey, TResult>( | |
this IEnumerable<TOuter> outer, | |
IEnumerable<TInner> inner, | |
Func<TOuter, TKey> outerKeySelector, | |
Func<TInner, TKey> innerKeySelector, | |
Func<TOuter, TInner, TResult> resultSelector) | |
{ | |
return from key in outer.Select(outerKeySelector).Union(inner.Select(innerKeySelector)) | |
join outerItem in outer on key equals outerKeySelector(outerItem) into outerGroup | |
from outerItem in outerGroup.DefaultIfEmpty() | |
join innerItem in inner on key equals innerKeySelector(innerItem) into innerGroup | |
from innerItem in innerGroup.DefaultIfEmpty() | |
where outerItem == null ^ innerItem == null | |
select resultSelector(outerItem, innerItem); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment