使用 LINQ ToDictionary 筛选自定义字典 ​​- “无法转换类型为“System.Collections.Generic.Dictionary`2”的对象”

发布于 2024-09-08 15:56:15 字数 1604 浏览 5 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2024-09-15 15:56:15

从 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.

十雾 2024-09-15 15:56:15

正如布伦南提到< /a>、MyDictionaryDictionary(Of Integer, String) 是完全不同的类型,即使其中一种类型派生于另一种类型。

ToDictionary 调用返回一个 Dictionary(Of Integer, String),然后您尝试将该字典转换为 MyDictionary

您可以创建自己的 ToMyDictionary 扩展方法来解决此问题:

Dim di2 As MyDictionary = _
    di1.Where(Function(w) w.Value.Contains("T")) _
       .ToMyDictionary(Function(tdk) tdk.Key, Function(tdv) tdv.Value)

' ...

<Extension()>
Public Shared Function ToMyDictionary(Of T)( _
    ByVal source As IEnumerable(Of T), _
    ByVal keySelector As Func(Of T, Integer), _
    ByVal valueSelector As Func(Of T, String)) As MyDictionary

    Dim result As New MyDictionary
    Dim item As T

    For Each item In source
        result.Add(keySelector.Invoke(item), valueSelector.Invoke(item))
    Next

    Return result
End Function

(如果语法不完全正确,抱歉:这些天我的 VB 非常生疏。)

As Brennan mentions, MyDictionary and Dictionary(Of Integer, String) are completely different types, even though one derives from the other.

The ToDictionary call returns a Dictionary(Of Integer, String) and you're then trying to cast that dictionary to a MyDictionary.

You could create your own ToMyDictionary extension method to workaround this:

Dim di2 As MyDictionary = _
    di1.Where(Function(w) w.Value.Contains("T")) _
       .ToMyDictionary(Function(tdk) tdk.Key, Function(tdv) tdv.Value)

' ...

<Extension()>
Public Shared Function ToMyDictionary(Of T)( _
    ByVal source As IEnumerable(Of T), _
    ByVal keySelector As Func(Of T, Integer), _
    ByVal valueSelector As Func(Of T, String)) As MyDictionary

    Dim result As New MyDictionary
    Dim item As T

    For Each item In source
        result.Add(keySelector.Invoke(item), valueSelector.Invoke(item))
    Next

    Return result
End Function

(Apologies if the syntax isn't entirely correct: my VB is very rusty these days.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文