Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.net中从字典中删除项_Vb.net_Dictionary_Foreach - Fatal编程技术网

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

在VB.net中从字典中删除项,vb.net,dictionary,foreach,Vb.net,Dictionary,Foreach,我有这样的想法: 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

我有这样的想法:

    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
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
然后,在字典中有一些数据,如:

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

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

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

可能有用。无需迭代列表元素来查找和删除它们,这可能会导致您遇到的困难。

您显示的代码不会产生任何错误,它可以正常工作。提供能够重现错误的真实代码示例。如果您使用的是IEnumerable?,只需复制集合,而不复制要删除的项即可。请参阅此处的备注:该代码看起来像是在保留“c2”并删除其余部分,而不是相反的部分。无需使用
ToList
甚至迭代列表,只要使用
If kvp.Value.Contains(“c2”)
谢谢您的回答@BrianHooper