[英]Sorting a dictionary by value with linq in vb.net
我正在尝试使用LINQ按值对字典进行排序,但是我无法弄清楚ToDictionary()方法的工作方式。
我可以找到的所有示例都在c#中。
这是我的代码
Dim f As Dictionary(Of String, String) = (From value In projectDescriptions.Values
Order By value Ascending
Select value).ToDictionary(???)
更新
最后,我才意识到那是愚蠢的。 抱歉
我做了一个列表(keyValuePair(字符串,字符串))
并排序我的清单
mylist.OrderBy(Function(x) x.Value).ToList()
希望对别人有帮助
您可以使用SortedDictionary吗?
同样在C#中,您想要实现的目标如下所示:
// enumerate dic itself, not just dic.Values
(from p in dic
orderby p.Value ascending
select new KeyValuePair<string, string>(p.Key, p.Value)
// or new { Key = p.Key, Value = p.Value })
.ToDictionary(p => p.Key, p => p.Value);
有什么相同
dic.OrderBy(p => p.Value).ToDictionary(p => p.Key, p => p.Value);
// or .ToDictionary(p => p.Key);
您说您没有找到此示例,但请在此处检查MSDN 。 在VB中应该可以执行以下操作。
'not sure what the key is, but use whichever property you'd like
Dim f As Dictionary(Of String, String) =
(From value In projectDescriptions.Values
Order By value Ascending
Select value).ToDictionary(Function(p) p.Key)
但是,如果将排序后的枚举器再次存储在字典中,它将变成未排序的 ,这通常是字典或地图的属性,而这正是字典如此快速和流行的原因。 如果您需要对它进行排序,也许您想使用SortedDictionary
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.