Vis*_*hal 1 c# linq asp.net-mvc-4
private Dictionary<string, Dictionary<string, string>> sort(Dictionary<string, Dictionary<string, string>> prods)
{
int a, b;
var sortedDict =new Dictionary<string, Dictionary<string, string>>();
sortedDict = from entry in prods orderby entry.Value["SortOrder"] ascending select entry;
return sortedDict;
}
Run Code Online (Sandbox Code Playgroud)
错误:
错误4无法将类型'System.Linq.IOrderedEnumerable >>'隐式转换为'System.Collections.Generic.Dictionary>'。存在显式转换(您是否缺少演员表?)
您可以ToDictionary用来创建字典-但实际上并不能实现您想要的。
一个Dictionary<,>根本不排序的数据结构。不应依赖订购。即使当前实现可能会按照添加顺序返回条目,但这是实现细节:
出于枚举的目的,字典中的每个项目都被视为
KeyValuePair<TKey, TValue>代表值及其键的结构。返回项目的顺序是不确定的。
您可能要看一下SortedDictionary<,>,但是鉴于排序顺序似乎取决于值而不是键,因此可能不可行。
根据您要执行的操作,您可能会声明您返回了一个 IEnumerable<KeyValuePair<string, Dictionary<string, string>>