繁体   English   中英

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

[英]How to filter a list of items whose their sum = someValue

我的预定义someValue是4,那么如何过滤或获取数量为Qty = 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之和的项目的新列表。一旦满足条件,就无需查找更多组合。 得到结果后,便可以处理新列表了

提前致谢。

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

谢谢,书呆子

    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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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