How can I use LINQ to “crop” a dictionary?

I have a dictionary and a list of keys to remove from the dictionary. This is my implementation right now:

var keys = (from entry in etimes
            where Convert.ToInt64(entry.Value) < Convert.ToInt64(stime)
            select entry.Key).ToList();

foreach (var key in keys)
{
    etimes.Remove(key);
    count--;
}

Is there something I can do to eliminate the foreach loop?

+5
source share
2 answers
var pruned = etimes.Where(entry => Convert.ToInt64(entry.Value) >= 
    Convert.ToInt64(stime)).ToDictionary(entry => entry.Key, 
        entry => entry.Value);

LINQ Where, , , ( , ). ToDictionary thwe IEnumerable<KeyValuePair<TKey, TValue>> Dictionary<TKey, TValue> , , , - - KeyValuePair ( ). , , / .

+6

, , :

<Extension> void itemDelete(List l, object item) {
    l.Remove(item)
}

var keys = (from entry in etimes
        where Convert.ToInt64(entry.Value) < Convert.ToInt64(stime)
        select entry.Key).ToList();

keys.foreach(itemDelete());

, 100% ( #); . .foreach LINQ. , VB, , # - ... .foreach. - , .

0

All Articles