I have two Lists of custom objects:
List1: Year, Month, ValueA
List2: Year, Month, ValueB
I want to get a third List with a merge between the two:
List3: Year, Month, ValueA, ValueB
Is there any elegant way to perform that in LINQ VB.Net?
Example:
List1:
2010 - 6 - 2
2010 - 7 - 5
2010 - 10 - 3
List2:
2010 - 7 - 2
2010 - 8 - 1
2010 - 10 - 2
List3 (result):
2010 - 6 - 2 - 0
2010 - 7 - 5 - 2
2010 - 8 - 0 - 1
2010 - 10 - 3 - 2
Thanks in advance.
Solution
VB.Net translation of the solution :
Dim ListA = From a In List1
Group Join b In List2
On a.Year Equals b.Year And a.Month Equals b.Month Into bGroup = Group
From b In bGroup.DefaultIfEmpty()
Select a.Year, a.Month, a.Value1, Value2 = If(b Is Nothing, 0, b.Value2)
Dim ListB = From b In List2
Group Join a In List1
On b.Year Equals a.Year And b.Month Equals a.Month Into aGroup = Group
From a In aGroup.DefaultIfEmpty()
Select b.Year, b.Month, Value1 = If(a Is Nothing, 0, a.Value1), b.Value2
Dim List3 = ListA.Union(ListB)
Sure, you’re looking to perform a Full Outer Join on the data, which doesn’t exist in LINQ, so we fake it with two unioned Left Outer Joins:
Many apologies for the C#, I could not get my VB.Net example to work for the life of me. You need two left outer joins to be unioned to produce the correct answer. None of the code converters I tried worked either.
The following is VB.Net that LINQPad chokes on, but every example I can find says should be correct: