Created
October 9, 2016 19:33
-
-
Save anonymous/0657c35eb9548c429238d2c8a92bfa0a to your computer and use it in GitHub Desktop.
This file contains 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
List<int> outer = new List<Int32> { 1, 2, 3, 4 }; | |
List<int> inner = new List<Int32> { 1, 6, 3, 8 }; | |
Console.WriteLine(outer.Count); | |
Console.WriteLine(inner.Count); | |
var result = outer.GroupJoin( | |
inner, // the inner list i.e. IEnumerable<TInner> | |
o => o, // We're selecting elements from the outer list in this case we just select them all | |
i => i, // We're selecting elements from the inner list in this case we just select them all | |
(o, g) => g.Select(c => new { Item1 = o, Item2 = c}) // This is our result function, it returns a tuple of each element if each element has an equal value | |
.DefaultIfEmpty(new { Item1 = o, Item2 = 0 })) // This speficies a default value for the tuple if the row doesn't match. | |
.SelectMany(g => g) // Flatten into one sequence | |
.ToList(); // Convert to a list | |
Console.WriteLine(result.Count); | |
// result should now contain the tuples | |
for (int i = 0; i < result.Count; i++) | |
Console.WriteLine("Item1: " + result.ElementAt(i).Item1 + " Item2: " + result.ElementAt(i).Item2); | |
Console.Read(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment