I have created a Dictionary class (MyDictionary for the example). I am currently trying to pass MyDictionary into a function, filter it into a new instance of MyDictionary and pass this new instance into another method.
When I am attempting to create the second instance from the filtered first instance of MyDictionary via Lambda Expressions and the ToDictionary Method, I am getting the following error:
Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.Int32,System.String]' to type 'MyDictionary'.
I have simplified the example and recreated it in LINQPad and am getting the same error.
Here's the simplified version of my code:
Sub Main
Dim di1 As New MyDictionary
di1(1) = "One"
di1(2) = "Two"
di1(3) = "Three"
di1(4) = "Four"
Dim di2 As MyDictionary= _
CType(di1.Where(Function(w) w.Value.Contains("T")) _
.ToDictionary(Function(tdk) tdk.Key, Function(tdv) tdv.Value), MyDictionary)
End Sub
Public Class MyDictionary
Inherits Dictionary(Of Integer, String)
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal dictionary As IDictionary(Of Integer, String))
MyBase.New(dictionary)
End Sub
End Class
Thanks in advance, -Aaron
Per Brennen's Post: I am able to do the following and it's fine, I was just hoping to not do it this way.
Sub Main
Dim di1 As New MyDictionary
di1(1) = "One"
di1(2) = "Two"
di1(3) = "Three"
di1(4) = "Four"
Dim di2 = _
di1.Where(Function(w) w.Value.Contains("T")) _
.ToDictionary(Function(tdk) tdk.Key, Function(tdv) tdv.Value)
Dim di3 As New MyDictionary(di2)
End Sub
Public Class MyDictionary
Inherits Dictionary(Of Integer, String)
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal dictionary As IDictionary(Of Integer, String))
MyBase.New(dictionary)
End Sub
End Class
Does anyone else have any thoughts on how I could skip the third MyDictionary instance?