合并LINQ中的2个列表 [英] Merging 2 Lists in LINQ
本文介绍了合并LINQ中的2个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个自定义对象列表:
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
在LINQ VB.Net中有什么优雅的方法可以执行该操作吗?
Is there any elegant way to perform that in LINQ VB.Net?
示例:
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
谢谢.
解决方案 VB.Net解决方案的翻译:
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:
var A = from a in List1
join b in List2 on new { a.Year, a.Month } equals new { b.Year, b.Month }
into bgroup
from b in bgroup.DefaultIfEmpty()
select new { a.Year, a.Month, a.ValueA, ValueB = (b == null ? 0 : b.ValueB) };
var B = from b in List2
join a in List1 on new { b.Year, b.Month } equals new { a.Year, a.Month }
into agroup
from a in agroup.DefaultIfEmpty()
select new { b.Year, b.Month, ValueA = (a == null ? 0 : a.ValueA), b.ValueB };
var List3 = A.Union(B);
为C#表示歉意,我无法使VB.Net示例在我的一生中都能正常工作.您需要将两个左外部联接合并以产生正确的答案.我尝试过的所有代码转换器都没有.
以下是LINQPad阻塞的VB.Net,但是我能找到的每个示例都应该是正确的:
The following is VB.Net that LINQPad chokes on, but every example I can find says should be correct:
Dim A = From a In List1 _
Group Join b In List2 _
On New With { a.Year, a.Month } Equals New With { b.Year, b.Month} _
Into bGroup = Group _
From b In bGroup.DefaultIfEmpty() _
Select a.Year, a.Month, a.ValueA, ValueB = If(b Is Nothing, 0, b.ValueB)
Dim B = From b In List2 _
Group Join a In List1 _
On New With { b.Year, b.Month } Equals New With { a.Year, a.Month} _
Into aGroup = Group _
From a In aGroup.DefaultIfEmpty() _
Select b.Year, b.Month, ValueA = If(a Is Nothing, 0, a.ValueA), b.ValueB
Dim List3 = A.Union(B)
这篇关于合并LINQ中的2个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文