Pregunta

How would I go about in deleting a specified key within a Dictionary based on the following condition?

foreach (var kvp in dict)
            {
                if (kvp.Key.Contains('/'))
                {
                    //delete the key
                }
            }
¿Fue útil?

Solución 5

EDIT: As said by the other answers below, what is important in the below code is calling ToList(). This will create a copy of dict which you will be able to iterate over while removing the items from dict without changing the collection you are iterating over.

You could do this by using something like

    dict.ToList().ForEach(a => { if (a.Key.Contains('/') dict.Remove(a.Key); });

Otros consejos

You would do it like this:

foreach(var keyToDelete in dict.Keys.Where(x => x.Contains('/')).ToList())
    dict.Remove(keyToDelete);

The important thing here is the call to ToList() after the Where. This will put all keys that should be deleted into a new list which you can iterate.
If you would try this code without the ToList() you would get an InvalidOperationException:

Collection was modified; enumeration operation may not execute.

Please note that this code is more efficient than the currently accepted answer. It only copies the keys that need to be deleted instead of the complete dictionary.

The problem is the fact that you can't modify a collection while looping over its elements. The solution is to put the keys to remove in a different collection and then looping over this to remove the elements. This is what Daniel's answer does.

If you can't/don't want to use LINQ, you can do it this way:

List<YourKeyType> toRemove = new List<YourKeyType>();
foreach (var kvp in dict)
{
    if (kvp.Key.Contains('/'))
        toRemove.Add(kvp.Key);
}
foreach (var aKey in toRemove)
    dict.Remove(aKey);

Depending on your data set, it might be more efficient to create a new object without the keys you want to discard:

dict = dict.Where(kvp => !kvp.Key.Contains('/'))
           .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

I could also argue that this is preferred to deleting keys since it communicates the intent of the code more clearly, but that is closely related to my personal coding style so your mileage may vary.

I realise this is already marked as answered, but I think that if you only want to delete a single key from a dictionary where the maximum size is not specified, making a copy of the ENTIRE dictionary just to remove one key is NOT a good solution!

To remove just one entry for a matching key, you can just do this:

foreach (var kvp in dict)
{
    if (kvp.Key.Contains('/'))
    {
        dict.Remove(kvp.Key);
        break;
    }
}

No copies of entire dictionaries required!

Note that this assumes there's only one key to be removed. If there might be more, use Daniel's or Francesco's answers above.

(Actually, I'll recommend you just use Daniel's answer, but I'll leave this here as an example without using Linq.)

Foreach will probably throw an exception. Try using for instead.

For deletion, use the Remove method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top