VB.NET2中的List.Find方法

时间:2015-06-10 11:14:25

标签: vb.net .net-2.0

在.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

填充列表后,我想找到具有匹配groupName的项目(只有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不允许这样做..

1 个答案:

答案 0 :(得分:2)

匿名函数(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)