小编Mar*_*rth的帖子

C# 将列表转换为字典

我正在尝试将 a 转换List<Ctrl>为 a Dictionary<string, Ctrl>usingCtrl.Name作为键。我已经参考了这个页面来尝试实现这一点:Microsoft's Enumerable.ToDictionary Method page

因此,我的代码如下所示:

Dictionary<string, Ctrl> oActionControls;

oActionControls = (from ctrl in _ctrls.Items
                   where ctrl.CtrlTypeCode == "BUTTON"
                   orderby ctrl.TabIndex
                   select ctrl).ToList().ToDictionary<string, Ctrl>(a => a, b => b.Name);
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息ToDictionary<string, Ctrl>(a => a, b => b.Name)实例参数:无法从“System.Collections.Generic.List<AppData.Ctrl>”转换为“System.Collections.Generic.IEnumerable”

参数 3:无法从“lambda 表达式”转换为“System.Collections.Generic.IEqualityComparer<AppData.Ctrl>”

参数 2:无法从“lambda 表达式”转换为“System.Func<string,AppData.Ctrl>”

'System.Collections.Generic.List<AppData.Ctrl>' 不包含 'ToDictionary' 的定义和最佳扩展方法重载 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable , System.Func<TSource,TKey>, System.Collections.Generic.IEqualityComparer)' 有一些无效的参数

不完全确定我在这里做错了什么。此外,对我来说,字典是按顺序声明的<key, value>,而 ToDictionary 构造函数按顺序采用 lambda 函数,这对我来说似乎很奇怪<value, key>

我该如何解决?我有点失落。谢谢。

编辑:添加到列表中,并包含所有编译时错误。

编辑 2:删除了 ToDictionary …

c# lambda todictionary

0
推荐指数
1
解决办法
62
查看次数

标签 统计

c# ×1

lambda ×1

todictionary ×1