Reputation: 55
I have something like this:
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
And then, have some data in dictionary as :
1, {"c1","c2", "c3"}
2, {"c2","c4", "c6"}
3, {"c3","c5", "c1"}
4, {"c4","c3", "c6"}
So to remove a specific item. Let say "c2" from the above dictionary. I am doing like this:
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
But it throws error : Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Can anyone please help me out. Thanks in advance!!
Upvotes: 2
Views: 6349
Reputation: 22054
I'd suggest something like
For Each channelKeyValuePair As KeyValuePair(Of Integer, List(Of String)) In Channel.PinnedChannelConnectionIds
channelKeyValuePair.Value.RemoveAll(Function(x) x = "C2")
Next
might work. There's no need to iterate over the list elements to find and remove them and it can cause difficulties of the sort you are experiencing.
Upvotes: 1