JHH JHH - 1 month ago 13
Vb.net Question

InvalidCastException Casting Dictionary Values

I'm trying to load a list of values from a Dictionary. I can iterate through the list and
get the values, but get an InvalidCastException


Additional information: Unable to cast object of type
'WhereListIterator`1[PumpTubing.Tubing]' to type 'PumpTubing.Tubing'.



when trying to use the following:

Dim tb2 As List(Of Tubing) = pd.pumps.Values.
Select(Function(f) f.
Where(Function(t) t.Tube.Equals("Tube3"))).
Cast(Of Tubing)().ToList()


Is there a way to do this? I've tried several variations and cannot get it to work.

I've included a method to Load some test data.

Following is my code:

Module Module1

Private pd As New PumpData

Sub Main()

LoadTestData()

' This works and returns a a List with 3 entries
Dim tb1 As New List(Of Tubing)
For Each x As KeyValuePair(Of Pumps, List(Of Tubing)) In pd.pumps
For Each t As Tubing In x.Value
If t.Tube.Equals("Tube3") Then tb1.Add(t)
Next
Next

' The following throws an InvalidCastExcption:
'
' Additional information: Unable to cast object of type
' 'WhereListIterator`1[PumpTubing.Tubing]' to type 'PumpTubing.Tubing'.
Dim tb2 As List(Of Tubing) = pd.pumps.Values.
Select(Function(f) f.
Where(Function(t) t.Tube.Equals("Tube3"))).
Cast(Of Tubing)().ToList()

End Sub

Private Sub LoadTestData()

pd.pumps.Add(New Pumps With {.Model = "Pump1", .MaxFlowRate = 300},
New List(Of Tubing) From {New Tubing With {.Tube = "Tube1", .VPR = 1.1},
New Tubing With {.Tube = "Tube2", .VPR = 1.2}})
pd.pumps.Add(New Pumps With {.Model = "Pump2", .MaxFlowRate = 400},
New List(Of Tubing) From {New Tubing With {.Tube = "Tube3", .VPR = 1.3},
New Tubing With {.Tube = "Tube4", .VPR = 1.4}})
pd.pumps.Add(New Pumps With {.Model = "Pump3", .MaxFlowRate = 500},
New List(Of Tubing) From {New Tubing With {.Tube = "Tube5", .VPR = 1.1},
New Tubing With {.Tube = "Tube6", .VPR = 1.2}})
pd.pumps.Add(New Pumps With {.Model = "Pump4", .MaxFlowRate = 600},
New List(Of Tubing) From {New Tubing With {.Tube = "Tube3", .VPR = 1.33},
New Tubing With {.Tube = "Tube7", .VPR = 1.4}})
pd.pumps.Add(New Pumps With {.Model = "Pump5", .MaxFlowRate = 700},
New List(Of Tubing) From {New Tubing With {.Tube = "Tube1", .VPR = 1.15},
New Tubing With {.Tube = "Tube8", .VPR = 1.2}})
pd.pumps.Add(New Pumps With {.Model = "Pump6", .MaxFlowRate = 800},
New List(Of Tubing) From {New Tubing With {.Tube = "Tube3", .VPR = 1.35},
New Tubing With {.Tube = "Tube9", .VPR = 1.4}})

End Sub

End Module


Here are the classes:

Public Class Pumps
Property Model As String
Property MaxFlowRate As Integer
End Class

Public Class Tubing
Property Tube As String
Property VPR As Decimal
End Class

Public Class PumpData
Property pumps As New Dictionary(Of Pumps, List(Of Tubing))
End Class

Answer

I'm not so good at VB syntax (coming from c#), but I guess you meant to use SelectMany instead of Select:

Dim tb2 As List(Of Tubing) = pd.pumps.Values.
                            SelectMany(Function(f) f.
                            Where(Function(t) t.Tube.Equals("Tube3"))).
                            Cast(Of Tubing)().ToList()