views:

50

answers:

1

I have a dictionary of type Dictionary<int, double> and want to eliminate values equal to a certain value.

At the moment I have

Dictionary <int, double> dict = GetDictionary();
dict = dict.Where(x => x.Value != 100).Select(x => x);

And am getting the error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<int,double>>' to 'System.Collections.Generic.Dictionary<int,double>'. An explicit conversion exists (are you missing a cast?)

How can I achieve what I'm trying to do using Linq?

+4  A: 

Using LINQ:

var newDict = dict.Where(x => x.Value != 100).ToDictionary(x => x.Key, x => x.Value);

The reason you are getting a compile error is that Select is an extension on IEnumerable. In the case of a Dictionary<TKey,TValue> you are dealing with an IEnumerable<KeyValuePair<TKey,TValue>>

erash
It works beautifully thank you.
Simon