none
Iterating and removing items from an IDictionary RRS feed

  • Question

  • I want to iterate through and remove keys from the IDictionary list being passed below.

    What is wrong with my code below? It errors after it removes the first key.     

    publicvoidBindAssignedTeams(IDictionarylist)

            {

               

    IDictionarylistToCompare = list;

               

    varkeys = listToCompare.Keys;

               

    foreach(varkey inkeys)

                {

                    list.Remove(key);

                }

    }


    CG

    Thursday, May 8, 2014 3:45 PM

Answers

  • The problem is that you can't modify a collection while you enumerate it, the solution provided by lapheal solves this by copying the keys to a separate array and enumerating that array. You can use his solution, you just need to put your "if (key.ToString() != "1")" line inside the foreach loop.

    You can make it simpler by using some enumerable extension methods:

    void BindAssignedTeams(IDictionary list) {
        foreach (var key in list.Keys.OfType<object>().Where(k => k.ToString() != "1").ToList())
            list.Remove(key);
    }

    But if you want to keep only one key and you know its type (say, string) then using Clear might be more efficient:

    public void BindAssignedTeams(IDictionary list) {
        if (list.Contains("1")) {
            var keep = list["1"];
            list.Clear();
            list["1"] = keep;
        }
        else {
            list.Clear();
        }
    }
    

    Thursday, May 8, 2014 6:40 PM
    Moderator

All replies

  • Hi, please try this:

    public void BindAssignedTeams(IDictionary list)
    {
    
        IDictionary listToCompare = list;
    
        object[] keys = new object[listToCompare.Keys.Count];
        listToCompare.Keys.CopyTo(keys, 0);
    
        foreach (var key in keys)
        {
    
            list.Remove(key);
    
        }
    }

    But if you want to remove everything simply, you can use list.Clear();

    Thursday, May 8, 2014 3:50 PM
  • Sorry. I should have said.

    I will only want to remove certain keys.

    See code below where I want to remove all keys except the key of 1.

           

    publicvoidBindAssignedTeams(IDictionarylist)

            {

               

    IDictionarylistToCompare = list;

               

    varkeys = listToCompare.Keys;

               

    foreach(varkey inkeys)

                {

                   

    if(key.ToString() != "1")

                    {

                        list.Remove(key);

                    }

                }


    CG


    • Edited by NuTech Thursday, May 8, 2014 4:06 PM
    Thursday, May 8, 2014 3:52 PM
  • The problem is that you can't modify a collection while you enumerate it, the solution provided by lapheal solves this by copying the keys to a separate array and enumerating that array. You can use his solution, you just need to put your "if (key.ToString() != "1")" line inside the foreach loop.

    You can make it simpler by using some enumerable extension methods:

    void BindAssignedTeams(IDictionary list) {
        foreach (var key in list.Keys.OfType<object>().Where(k => k.ToString() != "1").ToList())
            list.Remove(key);
    }

    But if you want to keep only one key and you know its type (say, string) then using Clear might be more efficient:

    public void BindAssignedTeams(IDictionary list) {
        if (list.Contains("1")) {
            var keep = list["1"];
            list.Clear();
            list["1"] = keep;
        }
        else {
            list.Clear();
        }
    }
    

    Thursday, May 8, 2014 6:40 PM
    Moderator