我遍历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
我已经编辑我的职位,因为下面是不是为我工作:对于每个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
如果你确实在'clsCellListExtender'中有'List'作为嵌套类的实现,那么扩展方法声明应该看起来像这样:Public Function GetSkypeCells(colCell As clsCellListExtender.List(Of T) )作为IEnumerable(Of clsCell)'。 *我认为*,这个命名与我的头真的很混乱。 –
我可能不得不把它放到一个实际的VB项目中,以便使嵌套类和泛型一切正常。几分钟后我会为你准备一些东西。 –