CodingInCircles
CodingInCircles

Reputation: 2807

How to loop through two lists simultaneously?

I have referred to the following question at: Using foreach loop to iterate through two lists. My question is this, with regards to the chosen answer: Can the o.DoSomething be a comparison? As in:

For Each a in ListA.Concat(ListB)
    If(a from ListA=a from ListB) Then
        Do Something here
    End If
Next

As you might've guessed, I'm using VB.Net and would like to know how I can do what I have shown here. That would basically be to iterate through a joined list separately/independently. Thanks!

Upvotes: 1

Views: 6586

Answers (4)

Dean
Dean

Reputation: 5946

The answers to my question is-it-possible-to-iterate-over-two-ienumerable-objects-at-the-same-time may help

  Dim listA As List(Of A)
            Dim listb As List(Of B)

            listA.Zip(listb, Function(a, b) a.property1 = b.property1).ForEach(AddressOf Print)   


Shared Sub Print(ByVal s As A)
    Console.WriteLine(s.Property1)
End Sub

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126992

Your question indicates that you need a Join operation, because it's not that you want to iterate over two lists, but you also want to match like items from one list to the other.

    Dim joinedLists = From item1 In list1 _
                    Join item2 In list2 _
                    On item1.Bar Equals item2.Bar _
                    Select New With {item1, item2}

    For Each pair In joinedLists
        'Do work on combined item here'
        'pair.item1'
        'pair.item2'
    Next

Other answers recommend Zip. That is simply a function that takes two sequences and produces a single result, much like join, but it is geared to work in a FIFO method over both lists. If you need connections made based on an equality, Join is the specifically built to be right tool for this job.

Upvotes: 5

Meta-Knight
Meta-Knight

Reputation: 17875

On solution would be to have a dictionary with items from the second list, then loop through your first list and retrieve the corresponding item in the second one, using the dictionary. Here's an example, assuming you want to compare items with an ID property:

Dim DictB = ListB.ToDictionary(Function(x) x.ID)
For each itemA in ListA
    If DictB.ContainsKey(itemA.ID)
        'Item is in both lists
        Dim itemB = DictB(itemA.ID)
        'Do something here
    End If
Next

Upvotes: 0

MarkJ
MarkJ

Reputation: 30408

In .Net 4 you could use Zip. Adapted from my answer to this question from a Python fan specifically asking for tuples - you could remove the tuples if you like.

Sub Main()
    Dim arr1() As String = {"a", "b", "c"} '' will also work with Lists
    Dim arr2() As String = {"1", "2", "3"}
    For Each t In TupleSequence(arr1, arr2)
        If t.Item1 = t.Item2 Then
          '' Do something
        End If
    Next
    Console.ReadLine()
End Sub
Function TupleSequence(Of T1, T2)(
    ByVal seq1 As IEnumerable(Of T1),
    ByVal seq2 As IEnumerable(Of T2)
    ) As IEnumerable(Of Tuple(Of T1, T2))
    Return Enumerable.Zip(seq1, seq2, 
      Function(s1, s2) Tuple.Create(s1, s2)
    )
End Function

Upvotes: 0

Related Questions