【发布时间】:2015-05-10 09:02:11
【问题描述】:
我有:
Dictionary<int, int> Color_Count = new Dictionary<int, int>();
还有:var sortedDict = from entry in Color_Count orderby entry.Value descending select entry;
但我不知道如何修复这个编译器错误。当我试图将键从这个字典复制到整数时,像这样:
int[] Colors_massive = sortedDict.Keys.ToArray();
导致错误 CS1061:
'System.Linq.IOrderedEnumerable<System.Collections.Generic.KeyValuePair<int,int>>' does not contain a definition for 'Keys' and no extension method 'Keys' accepting a first argument of type 'System.Linq.IOrderedEnumerable<System.Collections.Generic.KeyValuePair<int,int>>' could be found (are you missing a using directive or an assembly reference?)
如果我想复制,使用其他方法:
int[] Colors_massive = new int[sortedDict.Keys.Count];
sortedDict.Keys.CopyTo(Colors_massive, 0);
它也会导致同样的错误,但现在错误被打印了两次。如果我在代码中替换单词“Keys”,对于单词“Values”,它也会打印相同的错误,但现在编译器找不到“Values”的定义。
我在这里做错了什么?
【问题讨论】:
-
This 可以帮到你。
-
@Eminem Enumerable.ToDictionary 在这里没有帮助,因为他需要对其进行排序。
标签: c# arrays dictionary copy