Confusion over Linq/lambdas in VB.NET
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)
精彩评论