Sim*_*mon 2 .net c# linq dictionary
我有一个类型字典,Dictionary<int, double>
想要消除等于某个值的值.
目前我有
Dictionary <int, double> dict = GetDictionary();
dict = dict.Where(x => x.Value != 100).Select(x => x);
Run Code Online (Sandbox Code Playgroud)
我收到错误:
无法隐式将类型' System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<int,double>>
' 转换为' System.Collections.Generic.Dictionary<int,double>
'.存在显式转换(您是否错过了演员?)
如何使用Linq实现我想要做的事情?
cor*_*erm 12
使用LINQ:
var newDict = dict.Where(kvp => kvp.Value != 100)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Run Code Online (Sandbox Code Playgroud)
您收到编译错误的原因是Select是IEnumerable上的扩展.在Dictionary<TKey,TValue>
你正在处理的情况下IEnumerable<KeyValuePair<TKey,TValue>>