2016-10-14 8 views
0

ディクショナリから値のリストをロードしようとしています。私は、リストを反復処理し、 値を得るが、以下を使用しようとするとInvalidCastExceptionが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 

答えて

0

私は(C#のから来る)VBの構文ではあまり良くないんだけど、私はあなたが使用することに意味を推測しますの代わりに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() 
+0

それでした。どうも。 – JHH

関連する問題