在VB.NET中对Linq/lambdas的困惑

Goo*_*tan 2 .net c# vb.net

我有一个DTO部门的集合,并且只想提取所有Department对象的一个​​列.我知道在C#中你可以这样做:

List<Department> departments = corporation.GetDepartments();
List<int> departmentIDs = departments.ConvertAll(dep => dep.ID);
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这是使用LINQ,ConvertAll()的参数是lambda表达式.(?)

当试图在VB中实现相同时,似乎必须定义一个Converter方法:

Dim departmentIDs As List(Of Integer) = coll.ConvertAll(New Converter(Of Department, Integer)(AddressOf ConvertDepartmentToInt))

Public Shared Function ConvertDepartmentToInt(ByVal dep As Department) As Integer
    Return dep.ID
End Function
Run Code Online (Sandbox Code Playgroud)

为什么会这样?我错过了什么吗?

R. *_*des 7

您也可以在Visual Basic .NET中使用lambda.语法是这样的:

departments.ConvertAll(Function(dep) dep.ID)
Run Code Online (Sandbox Code Playgroud)

对于不返回值的lambda(如类型的lambda Action<int>),请Sub改用:

Sub(x) Console.WriteLine(x)
Run Code Online (Sandbox Code Playgroud)