无法将类型 'System.Collections.Generic.IEnumerable<AnonymousType#1>' 隐式转换为 'System.Collections.Hashtable'

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Hashtable'

我在尝试创建键值对并将其加载到哈希表时遇到以下错误

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Hashtable'. An explicit conversion exists (are you missing a cast?)

下面是代码片段

hashtable selectedValues = radTree.CheckedNodes.Where(node => node.Level == 0).Select(row => new
        {
            key = row.Value,
            Value = row.Text
        });

在上面的查询中,我试图在 Telerik RadTreeControl 中获取所选复选框(针对特定级别)的键和值,并将它们加载到哈希表中。

LINQ 中有没有一种方法可以将结果转换为散列表,或者我只使用字典更好,因为我们有 ToDictionary() 方法可用?

最好只使用字典。

否则你可以使用 ToDictionary and then pass that to the constructor of a hashtable 如果你真的想要。