Reputation: 11
The presented piece of code is used to find missing records in very large data set. Let say gA represents record numbers of correct data set. Another application delivers gB data set, which record numbers are supposed to be identical to gA. Even the number of missing records is small (19 against total of 36886) it prevents me from further calculations. The whole utility is written in mix of Lambda and LINQ so want to convert it to look the same...
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
Upvotes: 0
Views: 55
Reputation: 3115
The simple "translation" would be:
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);
}
});
})
However another approach would be using Contains
gA.ForEach(recA =>
{
if (gB.Contains(recA))
{
rA++;
}
else
{
rB++;
Console.WriteLine("{0} missing record: {1}", rB, recA);
}
})
Finally a cleaner and more concise option is using Except
and a ForEach
in the resulting collection.
gA.Except(gB).ForEach((item, idx) => Console.WriteLine("{0} missing record: {1}", idx, item));
Upvotes: 2
Reputation: 8591
Seems you're looking fot 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()
Above code produces a list of Integers
: {1, 3, 8}
Upvotes: 4