从 List(Of) 中获取符合特定条件的元素

Get elements with certain condition from List(Of)

我正在遍历 List(Of MyClass) 以查找具有特定条件的元素。

例如,在一种情况下,我需要找到所有这些元素并对它们进行处理:

    For Each nCell As clsCell In colCell
        If nCell.TempClickIndex = nCell.ClickIndex Then
            If nCell.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE Then

我想知道有没有办法简化这个。

我梦想着这样的事情:

For Each nCell As clsCell in colCell.GetSkypeCells()

调用 "GetSkypeCells" 将执行我上面所做的,并将在内部处理选择。

有办法吗?

编辑:

这是我的 colCell:

Public colCell As New clsCellListExtender.List(Of clsCell)

Imports System.Collections.ObjectModel

Public Class clsCellListExtender

Public Class List(Of T)
    Inherits Collection(Of T)

    Private _iID As Integer = 0
    Private i As Integer = 0

    Protected Overrides Sub InsertItem(index As Integer, item As T)
        'your checks here
        'i += 1
        'If i > 20000 Then
        '    i = 0
        'End If
        Debug.Assert(g_bCheck = False)

        If TypeOf (item) Is clsCell Then
            _iID += 1
            Dim nCell As clsCell = TryCast(item, clsCell)
            nCell.TempID = _iID
        End If

        MyBase.InsertItem(index, item)
    End Sub

End Class

End Class

试试这个:

For Each nCell As clsCell In colCell.FindAll(Function(c) c.TempClickIndex = c.ClickIndex And
                                                         c.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)

Next

您可以修改它并创建一个扩展方法,然后您可以用 colCell.GetSkypeCells()

调用它
<Extension>
Public Function GetSkypeCells(c As List(Of clsCell)) As List(Of clsCell)
    Return c.FindAll(Function(cc As clsCell) cc.TempClickIndex = cc.ClickIndex And
                                             cc.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
End Function

你可以使用这个:

For Each nCell as clsCell In colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
    'Do stuff with nCell
Next

对于您的 "dream" 解决方案,您可以向任何类型添加扩展方法 colCell 是 returns 上述 LINQ 的结果。

让它与嵌套 class 一起工作,泛型类型有点棘手,但我终于明白了。

Public Module Extensions
    <System.Runtime.CompilerServices.Extension()> _
    Public Function GetSkypeCells(Of T As clsCell)(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of T)
        Return colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
    End Function
End Module

这是一个带有有效扩展方法的小型控制台应用程序。我将实现留空以保存 space,但您应该可以根据上面的内容填写它。如果您有任何问题,请告诉我。

Imports System.Collections.ObjectModel
Imports System.Runtime.CompilerServices

Module Module1
    Sub Main()
        Dim a As New clsCellListExtender.List(Of clsCell)
        For Each cell As clsCell In a.GetSkypeCells()
            'Do things with cell here
        Next
    End Sub
End Module

Public Class clsCellListExtender
    Public Class List(Of T)
        Inherits Collection(Of T)
        Protected Overrides Sub InsertItem(index As Integer, item As T)
            '...
        End Sub
    End Class
End Class

Public Class clsCell
    '...
End Class

Module Extensions
    <Extension>
    Public Function GetSkypeCells(Of T As clsCell)(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of T)
        Return colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE)
    End Function
End Module

可以使用LINQ的扩展方法Where

Dim skypeCalls = 
    colCell.Where(Function(cell) cell.TempClickIndex = cell.ClickIndex).
           .Where(Function(cell) cell.StandardCellType = eStandardCellType.SCT_SKYPEMESSAG)

For Each skypeCall in skypeCalls
    ' Do something
Next