Hi everyone,
so i need to remove all entries in a dictionary accordingly to a specified lower bound.
My current solution is this:
List<string> keys = new List<string>();
foreach (KeyValuePair<string, int> kvp in dic)
{
if (kvp.Value < lowerBound)
keys.Add(kvp.Key);
}
foreach (string key in keys)
dic.Remove(key);
However this is rather expensive, especially since the size of the dictionary is rather large.
I've seen a LINQ solution like:
foreach(var kvp in dic.Where(kvp.Value <= lowerBound).ToDictionary())
{
dic.Remove(kvp.Key);
}
which i assume to be better since it's just 1 foreach, but i'm getting:
The name 'kvp' does not exist in the current context
The type arguments for method 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I admit i dont know anything about LINQ so any ideas how to make this 2nd solution work, or a better one?
Thnx in advance