List.Find VB.NET2 中的方法

List.Find Method in a VB.NET2

在 .NET2 中使用 VB

Public Class GroupSID 
    Private _groupName As String 
    Private _sid As String 
    Public Property GroupName() As String 
        Get 
            Return _groupName 
        End Get 
        Set(ByVal value As String) 
            _groupName = value 
        End Set 
    End Property 
    Public Property SID() As String 
        Get 
            Return _sid 
        End Get 
        Set(ByVal value As String) 
            _sid = value 
        End Set 
    End Property 
End Class

填充列表后我想找到具有匹配组名的项目(只有 1 个)

有点像(伪VB/C#)

'Dim result As GroupSID = ListOfGroupSID.Find(x => x.GroupName == groupName) 

https://msdn.microsoft.com/en-us/library/x0b5b5bc(v=vs.80).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

来自:http://www.codeproject.com/Articles/388257/Csharp-Tips-Using-delegate-in-List-Find-predicate

' expression expected error on Function(p) 
 Dim result As GroupSID = ListOfGroupSID.Find(Function(p) p.GroupName = groupName) 

问题是 VB8/.NET2 不允许这样做..

匿名函数 (lambda) 在 VB8/.Net2 中不可用,因此您必须将谓词定义为单独的方法:

Function BelongsToSameGroup(ByVal group As GroupSID) As Boolean
    Return group.GroupName = groupName ' need to be accessible
End Function

' usage
Dim result As GroupSID = ListOfGroupSID.Find(AddressOf BelongsToSameGroup)