简体   繁体   中英

Find Common Values in several arrays, or lists VB.NET

Find common values in multiple arrays with PHP

Pretty much does what I need, but in PHP, I need VB.

My situation is I am trying to create an intelligent stock pick system based on multiple stock locations.

At the point of invoicing, we loop through the items invoiced and check the stock database for available stock.

If all items on the invoice are available to pick from stock location 1, then all should be picked from stock location 1... etc.

If all most items are available from stock location 1 except one or two, then pick all stock from stock location 1 except for the exceptions which should be picked from the location with the highest available stock.

Finding highest available stock is simple, but I can't work out how to analyse the stock availability from multiple locations and finding the common stock locations.

I can create a set of arrays like this

Item ID    |     Available Stock Locations
    1      |        2, 3, 5
    2      |        1, 2, 6
    3      |        2, 3, 4
    4      |        1, 2 ,3 

How to I compare those location lists to find that 2 is common to all four?

Secondly, if one Item did not have a common stock location, how would I identify that item so I can go back and find the highest available stock level for it?

As in PHP, you can intersect arrays to find common values. Thanks to LINQ, this is fairly easy in VB:

Dim array1 = {2, 3, 5}
Dim array2 = {1, 2, 6}
Dim array3 = {2, 3, 4}
Dim array4 = {1, 2, 3}

Dim commonItems = array1.Intersect(array2).Intersect(array3).Intersect(array4)

commonItems is now an IEnumerable(Of Integer) containing all common store locations.

Linq would be cleaner, but this approach should work for 2.0...

Dim arrX(2) As Integer
arrX(0) = 0
arrX(1) = 1
arrX(2) = 2

Dim arrY(2) As Integer
arrY(0) = 0
arrY(1) = 32
arrY(2) = 2

Dim arrZ(2) As Integer
arrZ(0) = 10
arrZ(1) = 2
arrZ(2) = 22

Dim arrCommon() As Integer

For Each i As Integer In arrX

    For Each i2 As Integer In arrY

        If i = i2 Then

            For Each i3 As Integer In arrZ

                If i2 = i3 Then

                    If arrCommon Is Nothing OrElse arrCommon.Length = 0 Then

                        ReDim arrCommon(0)
                        arrCommon(0) = i

                    Else

                        ReDim Preserve arrCommon(arrCommon.Length)

                        arrCommon(arrCommon.Length - 1) = i

                    End If

                End If

            Next

        End If

    Next

Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM