繁体   English   中英

在VB.net中从字典中删除项目

[英]Removing item from Dictionary in VB.net

我有这样的事情:

    Public Shared Property PinnedChannelConnectionIds() As Dictionary(Of Integer, List(Of String))
    Get
        If _pinnedChannelConnectionIds Is Nothing Then
            Return New Dictionary(Of Integer, List(Of String))
        Else
            Return _pinnedChannelConnectionIds
        End If
    End Get
    Set(value As Dictionary(Of Integer, List(Of String)))
        _pinnedChannelConnectionIds = value
    End Set
End Property

然后,在字典中添加一些数据,如:

1,{“ c1”,“ c2”,“ c3”}
2,{“ c2”,“ c4”,“ c6”}
3,{“ c3”,“ c5”,“ c1”}
4,{“ c4”,“ c3”,“ c6”}

因此要删除一个特定的项目。 让我们从以上字典中说出“ c2”。 我这样做是这样的:

For Each channelKeyValuePair As KeyValuePair(Of Integer, List(Of String)) In Channel.PinnedChannelConnectionIds
            For Each item As String In channelKeyValuePair.Value.ToList()
                If (item.Equals("c2")) Then
                    Channel.PinnedChannelConnectionIds(channelKeyValuePair.Key).Remove(item)
                End If
            Next
        Next

但这会引发错误: 枚举数可用于读取集合中的数据,但不能用于修改基础集合。

谁能帮我一下。 提前致谢!!

我建议像

For Each channelKeyValuePair As KeyValuePair(Of Integer, List(Of String)) In Channel.PinnedChannelConnectionIds
    channelKeyValuePair.Value.RemoveAll(Function(x) x = "C2")
Next

可能有用。 无需遍历list元素即可查找和删除它们,这可能会给您遇到的困难带来麻烦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM