score:3

Accepted answer

Just use a loop to remove the items you don't want. You can write an extension method to make it easier to call:

public static class DictionaryExt
{
    public static void RemoveAll<K, V>(this IDictionary<K, V> dict, Func<K, V, bool> predicate)
    {
        foreach (var key in dict.Keys.ToArray().Where(key => predicate(key, dict[key])))
            dict.Remove(key);
    }
}

This usually will be more efficient than creating an entirely new dictionary, especially if the number of items being removed is relatively low compared to the size of the dictionary.

Your calling code would look like this:

runResults.RemoveAll((key, methodName) => methodName.Value.Where(method => method.outcome.ToLower().Equals("failed")));

(I chose the name RemoveAll() to match List.RemoveAll().)

score:0

To be honest you're probably better off selecting a new dictionary from the existing one:

runResults.Select().ToDictionary(x => x.Key, x => x.Value.Where(x => x.Value.outcome != "failed"));

*editted to reflect list in the dictionary.

Actually, you can get rid of the ones with no successful results by doing this too:

runResults.Select(x => new { x.Key, x.Value.Where(x => x.Value.outcome != "failed")} ).Where(x => x.Value.Any()).ToDictionary(x => x.Key, x => x.Value);

score:1

You could create a new dictionary by filtering out the invalid ones:

var filtered = runResults.ToDictionary(p => p.Key, p => p.Value.Where(m => m.outcome.ToLower() != "failed").ToList());

Ok, grrrrrr was faster :-)


Related Query

More Query from same tag