如何过滤总和为someValue的项目列表

时间:2019-01-19 14:36:37

标签: c# .net vb.net

我的预定义someValue是4,那么如何过滤或获取数量等于4的项目?

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})

我的预期结果是前两个项目,第一和第四项目或后三个项目。

编辑: 了解更多。我想要一个包含其qty = 4之和的项目的新列表。一旦满足条件,就无需查找更多组合。 得到结果后,就可以使用新列表进行工作

谢谢。

1 个答案:

答案 0 :(得分:0)

好的,我有一个可行的解决方案。我希望这与您要寻找的类似。

谢谢, 书呆子

    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