Skip to content

Instantly share code, notes, and snippets.

@gte445e
Created April 8, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gte445e/5efc8bf88a3d39fbfdcb9f35907b5f72 to your computer and use it in GitHub Desktop.
Save gte445e/5efc8bf88a3d39fbfdcb9f35907b5f72 to your computer and use it in GitHub Desktop.
LINQ Full Outer Join extension
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