开发者

Confusion over Linq/lambdas in VB.NET

开发者 https://www.devze.com 2023-03-13 07:48 出处:网络
I have a collection of a DTO, Department, and would like to extract only one of the columns for all the Department-objects. I know that in C# you can do like this:

I have a collection of a DTO, Department, and would like to extract only one of the columns for all the Department-objects. I know that in C# you can do like this:

List<Department> departments = corporation.GetDepartments();
List<int> departmentIDs = departments.ConvertAll(dep => dep.ID);

Out from my understanding this is use of LINQ, with the argument to ConvertAll() being a lambda expression. (?)

When trying to achieve the same in VB, it seems that one must define a Converter-method:

Dim departmentIDs As List(Of Integer) = coll.ConvertAll(New C开发者_StackOverflow社区onverter(Of Department, Integer)(AddressOf ConvertDepartmentToInt))

Public Shared Function ConvertDepartmentToInt(ByVal dep As Department) As Integer
    Return dep.ID
End Function

Why is it like this? Am I missing something?


You can use lambdas in Visual Basic .NET too. The syntax is like this:

departments.ConvertAll(Function(dep) dep.ID)

For lambdas that don't return a value (like those of type Action<int>), use Sub instead:

Sub(x) Console.WriteLine(x)
0

精彩评论

暂无评论...
验证码 换一张
取 消