开发者

Efficient way to remove all entries in a dictionary lower than a specified value

开发者 https://www.devze.com 2022-12-21 21:34 出处:网络
I need to remove all entries in a dictionary accordingly to a specified lower bound. My current solution is this:

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 i开发者_如何转开发n 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 don't know anything about LINQ so any ideas how to make this 2nd solution work, or a better one?


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.


Try this.

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


dic.Where(kvp.Value <= lowerBound)

replace with

dic.Where(kvp1 => kvp1.Value <= lowerBound)
0

精彩评论

暂无评论...
验证码 换一张
取 消