使用 LINQ ToDictionary 筛选自定义字典 - “无法转换类型为“System.Collections.Generic.Dictionary`2”的对象”
我创建了一个 Dictionary 类(例如 MyDictionary)。我目前正在尝试将 MyDictionary 传递到函数中,将其过滤到 MyDictionary 的新实例中,然后将此新实例传递到另一个方法中。
当我尝试通过 Lambda 表达式和 ToDictionary 方法从过滤后的 MyDictionary 的第一个实例创建第二个实例时,出现以下错误:
无法转换类型为 'System.Collections.Generic.Dictionary`2 的对象[System.Int32,System.String]' 来键入 'MyDictionary'。
我已经简化了示例并在 LINQPad 中重新创建了它,但遇到了相同的错误。
这是我的代码的简化版本:
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
提前致谢, -Aaron
Per Brennen 的帖子: 我能够执行以下操作,这很好,我只是希望不要这样做。
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
还有其他人对如何跳过第三个 MyDictionary 实例有任何想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 ToDictionary 返回的是字典,而不是 MyDictionary。 MyDictionary 是一个 Dictionary,但反之则不然,因此从 Dictionary 转换为 MyDictionary 是没有意义的。
换句话说,派生程度较低的类型无法自动转换为派生程度较高的类型。
What's being returned from ToDictionary is a Dictionary, not a MyDictionary. MyDictionary is a Dictionary, but not the other way around, so it doesn't make sense to cast from Dictionary to MyDictionary.
In other words, less-derived types can't automagically be converted to more-derived types.
正如布伦南提到< /a>、
MyDictionary
和Dictionary(Of Integer, String)
是完全不同的类型,即使其中一种类型派生于另一种类型。ToDictionary
调用返回一个Dictionary(Of Integer, String)
,然后您尝试将该字典转换为MyDictionary
。您可以创建自己的
ToMyDictionary
扩展方法来解决此问题:(如果语法不完全正确,抱歉:这些天我的 VB 非常生疏。)
As Brennan mentions,
MyDictionary
andDictionary(Of Integer, String)
are completely different types, even though one derives from the other.The
ToDictionary
call returns aDictionary(Of Integer, String)
and you're then trying to cast that dictionary to aMyDictionary
.You could create your own
ToMyDictionary
extension method to workaround this:(Apologies if the syntax isn't entirely correct: my VB is very rusty these days.)