Created
April 8, 2018 17:22
-
-
Save gte445e/5efc8bf88a3d39fbfdcb9f35907b5f72 to your computer and use it in GitHub Desktop.
LINQ Full Outer Join extension
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace System.Collections.Generic | |
{ | |
public static class IEnumerableExtensions | |
{ | |
public static IEnumerable<(T1 Left, T2 Right)> FullOuterJoin<T1, T2>(this IEnumerable<T1> left, IEnumerable<T2> right, Func<T1, T2, bool> match) | |
{ | |
var leftResults = from l in left | |
from r in right.Where(r => match(l, r)).DefaultIfEmpty() | |
select (Left: l, Right: r); | |
var rightResults = from r in right | |
from l in left.Where(l => match(l, r)).DefaultIfEmpty() | |
select (Left: l, Right: r); | |
return leftResults.Concat(rightResults).Distinct(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment