【发布时间】:2014-01-04 07:25:34
【问题描述】:
我正在尝试将哈希表转换为字典,并在这里发现了一个问题: convert HashTable to Dictionary in C#
public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)
{
return table
.Cast<DictionaryEntry> ()
.ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}
当我尝试使用它时,table.Cast 有错误; intellisense 不会将“Cast”显示为有效方法。
【问题讨论】:
-
C# 2 不支持 LINQ,所以你不能这样做。
-
你有
using System.Linq吗? -
谢谢塞巴斯蒂安;那是我的问题;我错过了“使用..”这一行。我现在拥有的代码行工作得很好。我也试过“字典
dict = HashtableToDictionary (htOffice);”我的哈希表使用字符串作为键,使用 int 作为值。不知道如何将您的回复标记为答案。
标签: c# .net dictionary hashtable c#-2.0