I have a list of objects of type Pump, each one of them holding a reference to an object of type Valve, exposed through its property Valve.
Public Class Pump
Private _valve As Valve 'may be Nothing
Public ReadOnly Property Valve() As Valve
Get
Return _valve
End Get
End Property
End Class
However, the valve can exist or just be Nothing.
It there an easy way to select all the valves and put them in a list?
I tried this:
_pumps.Select(Of Valve)(Function(p As Pump) p.Valve).ToList
But I get a list with some of the objects being Nothing, since there was no valve asigned to the correspondent pump.
I have finally done this:
Dim valves As New List(Of Valve)
For Each p As PumpIn _pumps
If p.Valve IsNot Nothing Then
valves.Add(p.Valve)
End If
Next
But I was wondering if there is a more compact way of doing it:
Thanks!