views:

168

answers:

3

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

A: 

Try this.

foreach(var kvp in dic.Where(x => x.Value <= lowerBound).ToDictionary())
{
    dic.Remove(kvp.Key);
}
Will
+7  A: 

Don't remove them explicitly, reassign the dictionary using only the values that are above that lower bound instead:

dic = dic.Where(kvp => kvp.Value > lowerBound).ToDictionary();

That way you can completely eliminate that foreach loop you've got in there.

Also, the reason you're getting that error is that you're not using the proper lambda syntax. In fact, you're not using a lambda at all. The expression:

kvp => kvp.Value > lowerBound

Is shorthand for a method that takes one parameter, called "kvp", and returns the evaluation of the expression:

kvp.Value > lowerBound

The => is called the "lambda operator", and it separates the parameter intake from the returned output. Every LINQ method that takes a Func is asking for a delegate of some sort, typically expressed as a lambda expression for brevity. When you give it the lambda expression, the compiler will stop complaining.

Read this for more information on lambda expressions.

David Morton
Only answer so far that looked at the problem, not the syntax error.
Benjamin Podszun
Thnx, after adding the arguments to ToDictionary(kvp => kvp.key, kvp => kvp.value) it works
brokencoding
I am surprised that this is efficient. I guess Linq is magic!
Moron
+1  A: 
dic.Where(kvp.Value <= lowerBound)

replace with

dic.Where(kvp1 => kvp1.Value <= lowerBound)
Sergej Andrejev