【发布时间】:2010-11-02 14:12:15
【问题描述】:
为了能够按值对字典进行排序,我使用以下代码:
Dim idCurrentJobs As IDictionary(Of String, Int32) = New Dictionary(Of String, Int32)
'The string in the dictionary represents a jobname and the integer is a counter for how many jobs im currently are running in the application'
idCurrentJobs.Add("JobName1", 2)
idCurrentJobs.Add("JobName2", 1)
idCurrentJobs.Add("JobName3", 2)
idCurrentJobs.Add("JobName4", 5)
idCurrentJobs.Add("JobName5", 3)
idCurrentJobs.Add("JobName6", 4)
Dim jobsSortedByCount As List(Of KeyValuePair(Of String, Int32)) = New List(Of KeyValuePair(Of String, Int32))(idCurrentJobs)
jobsSortedByCount.Sort(Function(firstPair As KeyValuePair(Of String, Int32), nextPair As KeyValuePair(Of String, Int32)) firstPair.Value.CompareTo(nextPair.Value))
idCurrentJobs = jobsSortedByCount.ToDictionary(Of List(Of KeyValuePair(Of String, Int32)))(Function(pair As KeyValuePair(Of String, Int32)) pair.Key)
当我使用 .ToDictionary 方法将 List 对象转换回 Directory 对象时,“pair.Key”出现错误:
'String' 类型的值无法转换为 'System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of String, Integer))
我应该如何使用 .ToDictionary 从我的对象列表中获取 Dictionary 对象?
如果我将使用 .ToDictionary 方法的行更改为:
idCurrentJobs = jobsSortedByCount.ToDictionary(Of KeyValuePair(Of String, Int32))(Function(pair As KeyValuePair(Of String, Int32)) pair)
由于“Strict On”,我收到此错误:
Option Strict On 不允许隐式 转换来自 'System.Collections.Generic.Dictionary(的 System.Collections.Generic.KeyValuePair(的 字符串,整数), System.Collections.Generic.KeyValuePair(的 字符串,整数))'到 'System.Collections.Generic.IDictionary(的 字符串,整数)'
我该如何解决这个问题?
【问题讨论】:
标签: .net vb.net list sorting dictionary