【问题标题】:Get elements with certain condition from List(Of)从 List(Of) 中获取特定条件的元素
【发布时间】:2018-02-09 10:35:26
【问题描述】:

我正在遍历 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

【问题讨论】:

    标签: vb.net list class extension-methods


    【解决方案1】:

    你可以用这个:

    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
    

    对于您的“梦想”解决方案,您可以将扩展方法添加到任何类型的 colCell 以返回上述 LINQ 的结果。

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

    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
    

    这是一个带有有效扩展方法的小型控制台应用程序。我将实现留空以节省空间,但您应该能够从上面的内容中填写它。如果您有任何问题,请告诉我。

    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
    

    【讨论】:

    • 我编辑了我的帖子,因为以下内容对我不起作用: For Each nCell As clsCell In colCell.GetSkypeCells 你能告诉我哪里出错了吗?编译器告诉我“GetSkypeCells 不是 clsCellListExnteder.List(Of clsCell) 的成员”。我使用了这样的扩展示例: Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of clsCell)) As IEnumerable(Of clsCell) Return colCell.Where(Function(x) x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType .SCT_SKYPEMESSAGE) 结束函数
    • 如果您确实有自己的List 实现作为clsCellListExtender 中的嵌套类,则扩展方法声明应类似于Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of clsCell)我觉得,这个名字真的让我头疼。
    • 我可能不得不把它放到一个实际的 VB 项目中,以使嵌套类和泛型的一切都正确。几分钟后我会给你一些东西。
    • 你让它工作了吗?我尝试了所有我能想象到的东西,但不知何故我没能做到。
    • 啊,你说得对,我错过了对T 的约束。我一定是在编辑和移动东西时丢掉了它。扩展方法应该类似于Public Function GetSkypeCells(Of T As clsCell)(colCell As clsCellListExtender.List(Of T)) As IEnumerable(Of T)。泛型类型需要限制为clsCells,以便它可以访问其成员。很抱歉给大家带来了困惑。
    【解决方案2】:

    试试这个:

    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
    

    【讨论】:

      【解决方案3】:

      可以使用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
      

      【讨论】:

        猜你喜欢
        • 2020-11-07
        • 2022-01-22
        • 2020-06-06
        • 1970-01-01
        • 2019-05-22
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多