2017-08-31 105 views
0

我遍历List(Of MyClass)以查找具有特定条件的元素。从List(Of)获取具有某些条件的元素

例如,在一种情况下,我需要找到所有这些元素,并做一些与他们:

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 

回答

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 
+0

我已经编辑我的职位,因为下面是不是为我工作:对于每个NCELL作为clsCell在colCell.GetSkypeCells你能告诉我在哪里我错了?编译器告诉我“GetSkypeCells不是clsCellListExnteder.List(Of clsCell)的成员”。我用你的扩展示例是这样的:Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of clsCell))As IEnumerable(Of clsCell) 返回colCell.Where(函数(x)x.TempClickIndex = x.ClickIndex AndAlso x.StandardCellType = eStandardCellType.SCT_SKYPEMESSAGE) End Function – tmighty

+1

如果你确实在'clsCellListExtender'中有'List'作为嵌套类的实现,那么扩展方法声明应该看起来像这样:Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of T) )作为IEnumerable(Of clsCell)'。 *我认为*,这个命名与我的头真的很混乱。 –

+1

我可能不得不把它放到一个实际的VB项目中,以便使嵌套类和泛型一切正常。几分钟后我会为你准备一些东西。 –

1

试试这个:

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 
0

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