给出以下两个字符串:
Dim str1() As String = {"123", "456", "789", "0"}
Dim str2() As String = {"123", "456", "1"}
我如何执行str1和str2的完全外部联接并最终具有如下结构:
{Nothing, "1"}
{"0", Nothing}
{"123", "123"}
{"456", "456"}
{"789", Nothing}
基于SO和其他网站上的一些讨论,我尝试使用LINQ:
Dim v = From a In str1 Join b In str2 On a Equals b Group By a Into g = Group
From o In g.DefaultIfEmpty()
但是它不会产生所需的结果,这与以下结果完全相同(常规的INNER JOIN):
Dim v = From a In str1 Join b In str2 On a Equals b
我看过的最后一个示例是here(C#)。
这是Another article,但是它似乎太复杂了,不可能成为最短的解决方案(我已经看到了更简单的C#示例,并希望VB能够同样高效)。
为什么这个问题有用
例如,可以具有一种文件结构,其中文件路径是关键。通过对键进行完全外部联接,您可以比较文件夹,查找哪一侧缺少哪些文件并向用户显示差异。任何类型的同步任务都可以使用此方法。
最佳答案
我想这不是您想要的解决方案,但是它似乎可以完成任务:
输出为:
string[] a1 = { "123", "456", "1" };
string[] a2 = { "123", "456", "789", "0" };
var intersection = a1.Intersect(a2); //get the intersection
var exceptions1 = a1.Except(a2); //get items from a1, that are not in a2
var exceptions2 = a2.Except(a1); //get items from a2, that are not in a1
var result = new List<Tuple<string, string>>(); //the result set
result.AddRange(intersection.Select(s => new Tuple<string, string>(s, s)));
result.AddRange(exceptions1.Select(s => new Tuple<string, string>(s, null)));
result.AddRange(exceptions2.Select(s => new Tuple<string, string>(null, s)));
foreach (var t in result)
{
Console.WriteLine((t.Item1 ?? "null") + "\t" + (t.Item2 ?? "null"));
}