Created
March 4, 2017 02:08
-
-
Save jboeke/b43e2146e3c8d660976971062073be47 to your computer and use it in GitHub Desktop.
C# Full Outer Join Into Object
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; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
public class ColorPrefs | |
{ | |
public string Color { get; set; } | |
public string LikedBy { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = new List<ColorPrefs>(); | |
var johnColors = new List<string>(new string[] { "red", "green", "purple", "orange", "yellow" }); | |
var janeColors = new List<string>(new string[] { "black", "red", "orange", "sage", "indigo" }); | |
var leftOuterJoin = from left in johnColors | |
join right in janeColors on left equals right | |
into grp | |
from g in grp.DefaultIfEmpty() | |
select new ColorPrefs | |
{ | |
Color = left, | |
LikedBy = left == g ? "BOTH" :"JOHN" | |
}; | |
var rightOuterJoin = from left in janeColors | |
join right in johnColors on left equals right | |
into grp | |
from g in grp.DefaultIfEmpty() | |
select new ColorPrefs | |
{ | |
Color = left, | |
LikedBy = left == g ? "BOTH" : "JANE" | |
}; | |
var unionedjoin = leftOuterJoin.Union(rightOuterJoin); | |
foreach(var item in unionedjoin) | |
{ | |
Console.WriteLine($"Color: {item.Color}, LikedBy: {item.LikedBy}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment