所呈现的代码片段用于在非常大的数据集中查找丢失的记录。假设 gA 表示正确数据集的记录号。另一个应用程序提供 gB 数据集,其记录数应与 gA 相同。即使丢失记录的数量很少(19对总共36886),它阻止我进一步计算。整个实用程序是以 Lambda 和 LINQ 的混合编写的,所以想把它转换成看起来一样......
Dim gA As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim gB As New List(Of Integer) From {2, 4, 5, 6, 7, 9}
Dim rA, rB As Integer
For Each recA As Integer In gA
Dim recB As Integer = gB(rA)
If recA = recB Then
rA += 1
Else
rB += 1
Console.WriteLine("{0} missing record: {1}", rB, recA)
End If
Next
' Output:
'1 missing record: 1
'2 missing record: 3
'3 missing record: 8
答案 0 :(得分:4)
好像你正在寻找照片Enumerable.Except() method。
Dim gA As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim gB As New List(Of Integer) From {2, 4, 5, 6, 7, 9}
DIm diff = gA.Except(gB).ToList()
以上代码会生成Integers
:{1, 3, 8}
答案 1 :(得分:2)
简单的“翻译”将是:
var gA = new List<Int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var gB = new List<Int>() { 2, 4, 5, 6, 7, 9 };
int rA, rB;
gA.ForEach(recA =>
{
gB.ForEach(recB =>
{
if (recA == recB)
{
rA++;
}
else
{
rB++;
Console.WriteLine("{0} missing record: {1}", rB, recA);
}
});
})
然而,另一种方法是使用Contains
gA.ForEach(recA =>
{
if (gB.Contains(recA))
{
rA++;
}
else
{
rB++;
Console.WriteLine("{0} missing record: {1}", rB, recA);
}
})
最后,更清晰,更简洁的选项是在结果集合中使用Except
和ForEach
。
gA.Except(gB).ForEach((item, idx) => Console.WriteLine("{0} missing record: {1}", idx, item));