-3
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

The above code works fine!!!

I want to use lambda expression for "For Each" loop.

Could anyone guide me with the lambda code, thanks!!!

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
Learner
  • 5
  • 1
  • 5
    It's not clear what you want and what didn't work. You can use `List.ForEach` or use a plain `For Each`-loop, but how is the lambda related at all? – Tim Schmelter Jan 19 '15 at 15:10
  • Do you mean using a LINQ statement in place of the `for each`? You say the above code works fine but the line `Temp2.Add(item.property1)` shouldnt work as `Temp2` is expecting a `Class2` but you're passing it a single property of `Temp` which doesnt even exist (`property1`). Colour me confused. – Jamiec Jan 19 '15 at 15:13
  • 1
    if it is not broke, dont fix it. do you want to use LINQ because all the cool kids are? – Ňɏssa Pøngjǣrdenlarp Jan 19 '15 at 15:15
  • I just want to change "for each" loop, may be like List.ForEach as Tim told – Learner Jan 19 '15 at 15:35

2 Answers2

1

You could use code like this (double check the syntax):

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

Although the For loop is pretty clear code and easy to maintain.

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
0

The quick way is:

Temp2.AddRange(Temp1.Select(Function(x) x.property1))
Axel
  • 3,331
  • 11
  • 35
  • 58
BagusWim
  • 18
  • 3