Как отфильтровать список предметов, у которых их сумма = someValue

mysqli_fetch_array или mysqli_result :: fetch_array (OOP) - это функция для результатов mysqli, а не связей mysqli.

Ваш $ result (объект mysqli_result) должен быть первым, и вероятно, только аргумент этой функции.

0
задан Leebeedev 19 January 2019 в 16:25
поделиться

1 ответ

Хорошо, у меня есть рабочее решение. Я надеюсь, что это похоже на то, что вы ищете.

Спасибо, ботаник

    Public Class MyObject
        Dim _Name As String
        Dim _Qty As Integer
        Public Property Name As String
            Get
                Return _Name
            End Get
            Set(value As String)
                _Name = value
            End Set
        End Property
        Public Property Qty As Integer
            Get
                Return _Qty
            End Get
            Set(value As Integer)
                _Qty = value
            End Set
        End Property
    End Class

    Public Sub Test3()
        Dim blnFoundMatch As Boolean
        Dim lngTotal As Long
        Dim lngI As Long
        Dim lngItm As Long
        Dim lngItm2 As Long
        Dim itm As MyObject
        Dim itm2 As MyObject

        Dim strOut As String

        Dim outList As New List(Of Object)
        Dim subList As New List(Of MyObject)

        Dim myList As New List(Of MyObject)
        myList.Add(New MyObject With {.Name = "N1", .Qty = 3})
        myList.Add(New MyObject With {.Name = "N2", .Qty = 1})
        myList.Add(New MyObject With {.Name = "N3", .Qty = 2})
        myList.Add(New MyObject With {.Name = "N4", .Qty = 1})
        myList.Add(New MyObject With {.Name = "N5", .Qty = 4})

        For Each itm In myList
            lngTotal = 0
            lngI = 0  
            lngItm = itm.Qty

            subList.Add(itm)
            lngTotal = lngItm
            If lngTotal = 4 Then
                outList.Add(New List(Of Object))
                For Each sL In subList
                    outList.Item(outList.Count - 1).add(sL)
                Next
            Else
                blnFoundMatch = False
                Do Until blnFoundMatch
                    itm2 = myList.Item(lngI)
                    lngItm2 = itm2.Qty

                    If Not itm Is itm2 Then
                        lngTotal += lngItm2
                        If lngTotal = 4 Then
                            blnFoundMatch = True
                            subList.Add(itm2)

                            'I had to do the following because a copy would not work
                            '  and I have to clear subList
                            outList.Add(New List(Of Object))
                            For Each sL In subList
                                outList.Item(outList.Count - 1).add(sL)
                            Next
                        ElseIf lngTotal > 4 Then
                            lngTotal -= lngItm2
                        Else
                            subList.Add(itm2)
                        End If
                    End If
                    lngI += 1
                    If (lngI > (myList.Count - 1)) Then Exit Do
                Loop
                subList.Clear()
            End If
        Next

        If outList.Count > 0 Then
            For Each obj As Object In outList
                strOut = "sumOf("
                For Each itm In obj
                    strOut = strOut & itm.Name & "  "
                Next
                strOut = Trim(strOut) & ") = 4"
                Debug.Print(strOut)
            Next
        End If

        'Results Are as Follows (Notice the duplicate, but it works):
        '-------------------------------------------------------------
        'sumOf(N1  N2) = 4
        'sumOf(N2  N1) = 4
        'sumOf(N3  N2  N4) = 4
        'sumOf(N4  N1) = 4
        'sumOf(N5) = 4 

    End Sub
0
ответ дан IAmNerd2000 19 January 2019 в 16:25
поделиться