- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了能够按值对字典进行排序,我使用了以下代码:
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”上收到错误消息:
Value of type 'String' cannot be converted to 'System.Collections.Generic.List(Of System.Collections.Generic.KeyValuePair(Of String, Integer))
我应该如何使用 .ToDictionary 从我的对象列表中获取字典对象?
如果我使用 .ToDictionary 方法将行更改为:
idCurrentJobs = jobsSortedByCount.ToDictionary(Of KeyValuePair(Of String, Int32))(Function(pair As KeyValuePair(Of String, Int32)) pair)
我收到这个错误是因为“Strict On”:
Option Strict On disallows implicit conversions from 'System.Collections.Generic.Dictionary(Of System.Collections.Generic.KeyValuePair(Of String, Integer), System.Collections.Generic.KeyValuePair(Of String, Integer))' to 'System.Collections.Generic.IDictionary(Of String, Integer)'
我该如何解决这个问题?
最佳答案
即使在 Option Strict On
的情况下,这也会起作用。
Dim list As List(Of KeyValuePair(Of String, Int32))
Dim dict As IDictionary(Of String, Int32) =
list.ToDictionary(Function(p) p.Key, Function(p) p.Value)
问题就出在你的代码上:
ToDictionary(Of List(Of KeyValuePair(Of String, Int32)))
关于.net - 使用 .ToDictionary 将 List(Of KeyValuePair(Of String,Int32)) 转换为 Dictionary(Of String, Int32),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4078513/
我有一个 List> .我想知道 KeyValuePair谁拥有最大值 Key . 但是,我想获取 key 的值。不仅是 key 。 我能做到: int myValue = myListe.Max(
C#我在我的主 KeyValuePair 中使用 KeyValuePair 作为键。这是我的问题: 如果我有“KeyValuePair”,声明如下: KeyValuePair varName = ne
在 C# 中,如何将 KeyValuePair 转换为 KeyValuePair?我尝试了以下但它不起作用: public static KeyValuePair DoTheCast(KeyValue
以这种糟糕的数据结构结束: List>> 它不太可能变得很大(我估计 > 这很容易在 .NET 2.0 中编写 - 它基本上只是一个值的三元组,而不是在 KeyValuePair 中有 2 个。不过,
查看System.Collections.Generic.Dictionary , 它清楚地实现了 ICollection> ,但没有所需的“void Add(KeyValuePair item)”功
是否可以使用自定义结构来代替 KeyValuePair? 而不是 For Each kvp As KeyValuePair(Of class1,class2) In myDict Dim c1 A
我们如何验证 (.NET 2) 如果 KeyValuePair是否已分配值? Dim pair as KeyValuePair(Of A, B) = Nothing if pair.??? Then
我的 list返回 显然。如何在我的 KeyValuePair 中附加第二个字符串并添加我从第二次数据库调用返回的 ID? // Database calls here KeyValu
编辑:文化 这些内部操作表明不变文化 OrdinalIgnoreCase最合适。 KeyValuePair 是密封的。如果我有 var a = KeyValuePair("foo",1)和 var b
我正在用 C#/.NET 4.5 编写一个应用程序,我有一个定义如下的字典: Dictionary d = new Dictionary(); 我的整数键是连续且唯一的,除了空字符串外,我的值也将是唯
我正在执行 LINQ 查询以从数据库、年龄和日期中获取 2 个字段中的所有值。我想在图表上显示 5 个最高年龄的数字。为此,我试图将年龄和日期存储在 2 个单独的列表 ageList 和 dateLi
我有一个从数据库中填充的字典。 如果只返回一条记录,如何访问KeyValuePair? 我有这个代码: Dictionary CandidatePlatypiDict = PlatypusID > 0
假设我有 List> d 我知道字符串,但我想找到它的整数。如何在此列表中找到键值对? 最佳答案 你会这样写: var result = d.Where(kvp => kvp.Value == "s
我有一个具有 KeyValuePair 类型属性的对象。 我想从数据库中读取一些数据并将结果存储在这个 KeyValuePair 类型字段中。 myObject.KeyValuePairs = ctx
我有一个 IEnumerable对于具有 string 的实体和一个 int成员。 我需要将其转换为 KeyValuePair 的数组反之亦然。 它因转换错误而失败。 [DataContract] p
我有一个 IEnumerable> keyValueList 类型的对象, 我正在使用 var getResult= keyValueList.SingleOrDefault(); if(getR
我正在 build 一个 Dictionary带 key 型KeyValuePair .使用时KeyValuePair.Create内Dictionary初始化程序它不需要模板类型并且它正确地推断了类
我有一个列表 KeyValuePair在 C# 中格式为 KeyValuePair .我想从列表中删除具有重复值的项目。 具有 {X,Y} 的 Point 对象坐标。 示例数据: List> Data
我有这个代码: List>> aux = retEmpty.ToList(); aux.ForEach(x => ret.Add(x.Key, x.Value)); 我需要按第一个元素(“string
我有一个方法需要以下内容: public static List ParetoBuildBySum(List> inputData) 我有以下 linq 查询,并希望传入 KeyValueP
我是一名优秀的程序员,十分优秀!