bat3a bat3a - 1 year ago 46
Vb.net Question

multidimensional array linq query

i have 5 dimensional array, when using linq to query, the results are sorted in dimensional way:

array(a)(b)(c)(d)(e) , dimension = 1

using for next:

For e = 0 To dimension - 1
For d = 0 To dimension - 1
For c = 0 To dimension - 1
For b = 0 To dimension - 1
For a = 0 To dimension - 1
listbox.Items.Add(array(a, b, c, d, e).disc)
Next
Next
Next
Next
Next


this would result in:

abcde

00000

10000

01000

...

if i use linq:

listbox.Items.AddRange((From item In array Select item.disc).ToArray)


this would result in:

abcde

00000

00001

00010

...

how can i achieve the first result with linq?

Answer Source
Public Class ArrayExt

    Public Shared Function GetFirstFastestEnumerator(Of T)(source As Array) As IEnumerable(Of T)
        Dim srcRank = source.Rank
        Dim indices = New Integer((srcRank) - 1) {}
        Dim len = source.Length

        For i = 0 To srcRank - 1
            indices(i) = source.GetLowerBound(i)
        Next

        Dim curRank As Integer = 0
        For i = 0 To len - 1
            Return CType(source.GetValue(indices), T)

            While (curRank < srcRank)
                If (indices(curRank) < source.GetUpperBound(curRank)) Then
                    indices(curRank) = indices(curRank) + 1
                    curRank = 0
                    Exit While
                Else
                    indices(curRank) = source.GetLowerBound(curRank)
                    curRank = curRank + 1
                End If

            End While
        Next

    End Function
End Class
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download