我有一个DTO部门的集合,并且只想提取所有Department对象的一个列。我知道在C#中你可以这样做:
List<Department> departments = corporation.GetDepartments();
List<int> departmentIDs = departments.ConvertAll(dep => dep.ID);
根据我的理解,这是使用LINQ,ConvertAll()的参数是lambda表达式。 (?)
当试图在VB中实现相同的功能时,似乎必须定义一个Converter-method:
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
为什么会这样?我错过了什么吗?
答案 0 :(得分:7)
您也可以在Visual Basic .NET中使用lambdas。语法如下:
departments.ConvertAll(Function(dep) dep.ID)
对于不返回值的lambda(如Action<int>
类型的lambdas),请改用Sub
:
Sub(x) Console.WriteLine(x)