对于vb.net中的每个lambda表达式

时间:2015-01-19 15:07:27

标签: vb.net lambda

Partial Public Class class2
        Public Property id As Long
        Public Property Name As String
        Public Property Type As String
        Public Property Value As String
        Public Property Date As Nullable(Of Date) 
End Class

Private Class Class1            
    Public Property property1 As class2
End Class


Dim Temp1 As New List(Of Class1)
Dim Temp2 As New List(Of Class2)

For Each item In Temp1
    Temp2.Add(item.property1)
Next

以上代码工作正常!!!

我想将lambda表达式用于“For Each”循环。

有人可以用lambda代码指导我,谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用这样的代码(仔细检查语法):

Dim Temp2 As List(Of Class2) = Temp1.Select(Function(i) i.property1).ToList()

虽然For循环代码非常清晰,易于维护。

答案 1 :(得分:0)

快速的方法是:

Temp2.AddRange(Temp1.Select(Function(x) x.property1))