473,383 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Dictionary, Enumeration and Remove

I have a type

Dictionary<int, List<int>> oKP = new Dictionary<int, List<int>>();

Im populating the dictionary so that a key is associated with a list of
integers. I then remove some, or possible all integers from the list by using
foreach:

foreach (int i in oKP.Keys)
{
foreach (int j in oKP[i])
{
if (oKP.ContainsKey(j))
{
if (oKP[j].Contains(i)) oKP[j].Remove(i);
}
}
}

I'd like to add a test which determines if the list is now empty
(oKP[i].Count == 0) and if so deletes the key and list from the dictionary -
like so:

foreach (int i in oKP.Keys)
{
foreach (int j in oKP[i])
{
if (oKP.ContainsKey(j))
{
if (oKP[j].Contains(i)) oKP[j].Remove(i);
if (0 == oKP[j].Count) oKP.Remove(j);
}
}
}

However when the remove of the dictionary entry is actioned I get an error:

Collection was modified; enumeration operation may not execute.

Now I understand why this happens, my question is how do I iterate over the
Dictionary and remove entries with a count of 0? What alternative can I use
to foreach that allows me to delete an entry?

Thanks in advance
Nov 22 '05 #1
1 5175
This is because you are not supposed to alter the collection while going
through it using an enumerator (which is what for each is doing behind the
scenes).
Solution: use a for or while loop.

"Dave Arkley" <Dave Ar****@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
I have a type

Dictionary<int, List<int>> oKP = new Dictionary<int, List<int>>();

Im populating the dictionary so that a key is associated with a list of
integers. I then remove some, or possible all integers from the list by using foreach:

foreach (int i in oKP.Keys)
{
foreach (int j in oKP[i])
{
if (oKP.ContainsKey(j))
{
if (oKP[j].Contains(i)) oKP[j].Remove(i);
}
}
}

I'd like to add a test which determines if the list is now empty
(oKP[i].Count == 0) and if so deletes the key and list from the dictionary - like so:

foreach (int i in oKP.Keys)
{
foreach (int j in oKP[i])
{
if (oKP.ContainsKey(j))
{
if (oKP[j].Contains(i)) oKP[j].Remove(i);
if (0 == oKP[j].Count) oKP.Remove(j);
}
}
}

However when the remove of the dictionary entry is actioned I get an error:
Collection was modified; enumeration operation may not execute.

Now I understand why this happens, my question is how do I iterate over the Dictionary and remove entries with a count of 0? What alternative can I use to foreach that allows me to delete an entry?

Thanks in advance

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Arkley | last post by:
I have a type Dictionary<int, List<int>> oKP = new Dictionary<int, List<int>>(); Im populating the dictionary so that a key is associated with a list of integers. I then remove some, or...
6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
1
by: Dan Holmes | last post by:
I have a custom dictionary and i would like to bind it to a control. Since Dictionary doesn't implement IList i would have to write it. Is the only way to do this to keep another collection that...
2
by: tamara.roberson | last post by:
I am porting some code to use generics. Currently, we have a synchronized Hashtable called as follows: private Hashtable table = Hashtable.Synchronized (new Hashtable ()); What I would like...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.