繁体   English   中英

按顺序遍历对象列表-vb.net

[英]Iterate in order through a list of objects - vb.net

我有一个名为Machines的对象列表。 每台机器都有一个“ Priority ”属性(UInteger)。 我试图以特定顺序(从最低优先级到最高优先级)从此列表中选择计算机(即将它们添加到另一个列表),直到满足条件为止。

因此,我想知道以下方法是否适用:

MachineList.Sort(Function(x, y) x.Priority.CompareTo(y.Priority))
For Each machin In MachineList
            MachinesToDisableList.AddMachine(machin)
            If [Condition] Then
                Exit For
            End If
Next

目的是将计算机从最低优先级到最高优先级添加到第二个列表,并在足够时停止。 但是我不确定我的解决方案是否会以正确的顺序进行迭代以获得预期的结果。

那好吗? 还是解决方案在别的地方?

为什么不只是遍历优先级排序的列表呢? 像这样:

    Dim machineList As New List(Of Machine)
    Dim machinesToDisableList As New List(Of Machine)
    machineList.Add(New Machine With {.Priority = 6})
    machineList.Add(New Machine With {.Priority = 2})
    machineList.Add(New Machine With {.Priority = 10})

    For Each machine In machineList.OrderBy(Function(x) x.Priority)
        machinesToDisableList.Add(machine)
        If [Condition] Then
            Exit For
        End If
    Next

暂无
暂无

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

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