How can I Remove items from dictionary using lambda expression

The simplest way is probably to create a new dictionary, if that’s okay for you:

var newDictionary = oldDictionary.Where(pair => pair.Value >= 0)
                                 .ToDictionary(pair => pair.Key,
                                               pair => pair.Value);

If you have to mutate the existing dictionary (e.g. because several other objects have reference to the same dictionary) you’d need to build a list of keys to remove, then remove them afterwards:

var toRemove = dictionary.Where(pair => pair.Value < 0)
                         .Select(pair => pair.Key)
                         .ToList();

foreach (var key in toRemove)
{
    dictionary.Remove(key);
}

EDIT: I’ve just noticed the first sentence: “I am not into LINQ solutions”. If that means you don’t want to use a LINQ solution, here’s the by-hand version:

List<int> toRemove = new List<int>();
foreach (KeyValuePair<int, int> pair in dictionary)
{
    if (pair.Value < 0)
    {
        toRemove.Add(pair.Key);
    }
}

foreach (var key in toRemove)
{
    dictionary.Remove(key);
}

… but if you can use LINQ, I’d encourage you do. My second solution is equivalent to the “by-hand” version, but more readable IMO.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)