Is it possible to iterate over two IEnumerable objects at the same time?

前端 未结 7 1953
醉梦人生
醉梦人生 2020-12-19 12:55

If I have a List(Of x) and a List(Of y) is it possible to iterate over both at the same time?

Something like

for each _x as X, _y as Y in List(of x         


        
7条回答
  •  误落风尘
    2020-12-19 13:19

    No, not in vb.net with the vb loop constructs.

    You can however do it yourself with the enumerators:

        Sub MyOwnIENumeration()
        Dim a As List(Of String), b As List(Of String)
        Dim EnmA As System.Collections.Generic.IEnumerator(Of String) = a.GetEnumerator
        Dim EnmB As System.Collections.Generic.IEnumerator(Of String) = b.GetEnumerator
    
        If EnmA.MoveNext() And EnmB.MoveNext() Then
            Do
                If EnmA.Current = EnmB.Current Then
                    Debug.Print("list matched on " & EnmA.Current)
                    If Not EnmA.MoveNext() Then Exit Do
                    If Not EnmB.MoveNext() Then Exit Do
                ElseIf EnmA.Current < EnmB.Current Then
                    If Not EnmA.MoveNext() Then Exit Do
                Else
                    If Not EnmB.MoveNext() Then Exit Do
                End If
            Loop
        End If
    
        EnmA.Dispose() : EnmB.Dispose()
    End Sub
    

提交回复
热议问题