InvalidCastException转换字典值

时间:2016-10-14 16:51:05

标签: vb.net linq

我正在尝试从Dictionary中加载值列表。我可以遍历列表和 获取值,但获取InvalidCastException

  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()

有办法做到这一点吗?我尝试了几种变体而无法使其发挥作用。

我已经加入了一种加载一些测试数据的方法。

以下是我的代码:

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

以下是课程:

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

1 个答案:

答案 0 :(得分:0)

我不太擅长VB语法(来自c#),但我想你的意思是使用SelectMany代替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()
相关问题