【问题标题】:How to filter a list of items whose their sum = someValue如何过滤总和 = someValue 的项目列表
【发布时间】:2019-01-19 14:36:37
【问题描述】:

我预定义的 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})

我的预期结果将是前两项项、第 1 项和第 4 项项或后三项项。

编辑: 为了更多的理解。我想要一个包含数量总和 = 4 的项目的新列表,一旦满足条件,就无需找到更多组合。 得到结果后,我就需要对新列表进行一些工作了

提前致谢。

【问题讨论】:

  • 您希望它们出现在新列表中吗?就像List(Of List(Of MyObject))?您将有一个列表,但每个列表中有一组 MyObject...
  • 是的,我想要一个新的 MyObject 列表,其中仅包含总数量为 4 的项目。
  • 您想要所有有效的组合还是任何一种都可以?
  • 我真的很好奇这有什么用处。通常,当您关心总和时,您会按某个键进行分组。如果您有 300 件商品,数量范围在 1 到 3 之间怎么办?
  • 这个问题太笼统了。你试过什么?

标签: c# .net vb.net


【解决方案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

【讨论】:

    猜你喜欢
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多