【发布时间】:2010-06-17 22:46:53
【问题描述】:
我关注了answer to another question,我得到了:
// itemCounter is a Dictionary<string, int>, and I only want to keep
// key/value pairs with the top maxAllowed values
if (itemCounter.Count > maxAllowed) {
IEnumerable<KeyValuePair<string, int>> sortedDict =
from entry in itemCounter orderby entry.Value descending select entry;
sortedDict = sortedDict.Take(maxAllowed);
itemCounter = sortedDict.ToDictionary<string, int>(/* what do I do here? */);
}
Visual Studio 要求提供参数Func<string, int> keySelector。我尝试按照我在网上找到的一些半相关示例并输入k => k.Key,但这会产生编译器错误:
'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,int>>'不包含“ToDictionary”的定义和最好的 扩展方法重载'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)'有一些无效参数
【问题讨论】:
标签: c# linq dictionary