自定义集合/枚举 - 如何过滤返回的集合?

时间:2014-03-31 14:09:15

标签: vb.net class collections enumeration

目前我有以下代码......

For Each Ord as clsOrder in Orders
   If NOT Ord.Status = voided then
     'do stuff to all orders that are not voided...
   endif
Next

我想知道的是,是否有办法让返回/枚举的集合甚至不返回无效的项目,所以我不需要嵌套的if语句来避免这些?某种自定义枚举器或什么?

谢谢, 道格

2 个答案:

答案 0 :(得分:0)

您可以使用Where clause过滤集合,例如

For Each Ord as clsOrder in Orders.Where(Function(o) Ord.Status != voided)
    ...
Next

答案 1 :(得分:0)

您可以使用这样的Lambda expression Where子句(在.NET 3.5及更高版本中可用):

Public Class clsOrder
    Public Property Status As Integer
End Class

Dim Orders As New List(Of clsOrder)

'populate Orders here

For Each Ord As clsOrder In Orders.Where(Function(x) x.Status <> voided)

Next

请注意,这并不比您发布的方式更快,但它更容易阅读