【问题标题】:C# Convert a List to a DictionaryC# 将列表转换为字典
【发布时间】:2021-09-28 16:00:02
【问题描述】:

我正在尝试使用 Ctrl.Name 作为键将 List<Ctrl> 转换为 Dictionary<string, Ctrl>。我参考了这个页面来尝试实现这一点: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);

我在ToDictionary&lt;string, Ctrl&gt;(a =&gt; a, b =&gt; b.Name) 上收到错误消息,内容如下: 实例参数:无法从 'System.Collections.Generic.List' 转换为 'System.Collections.Generic.IEnumerable'

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

参数 2:无法从 'lambda 表达式' 转换为 'System.Func'

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

不完全确定我在这里做错了什么。另外,对我来说,字典以&lt;key, value&gt; 的顺序声明,而 ToDictionary 构造函数以&lt;value, key&gt; 的顺序使用 lambda 函数,这对我来说似乎很奇怪。

我该如何解决这个问题?我有点失落。谢谢。

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

编辑 2: 删除了 ToDictionary 的类型参数。我还删除了多余的 ToList() 调用,因此代码如下:

oActionControls = (from ctrl in _ctrls.Items
                   where ctrl.CtrlTypeCode == "BUTTON"
                   orderby ctrl.TabIndex
                   select ctrl).ToDictionary(a => a.Name);

正常工作。

【问题讨论】:

  • 您的代码中是否缺少using System.Linq;?
  • .ToDictionary&lt;string, Ctrl&gt;(b =&gt; b.Name, b =&gt; b);?
  • 不,System.Linq 在里面。错误出现在 ToList().ToDictionary&lt;string, Ctrl&gt;(a =&gt; a, b =&gt; b.Name) 而不是 Linq 语句上。
  • 字典中的键是字符串,值是控件,但您的 ToDictionary 调用却相反。
  • 哦,你也有错误的 lambda 表达式:.ToDictionary(a =&gt; a.Name, b =&gt; b)(注意你不需要指定泛型类型,因为它们可以被推断出来。

标签: c# lambda


【解决方案1】:

假设您有一个List&lt;Ctrl&gt;,您可以通过使用键选择器参数调用.ToDictionary() 来创建字典(请参阅MS docs):

Dictionary<string, Ctrl> actionControls = ctrls.ToDictionary(ctrl => ctrl.Name);

确保您有 using System.Linq; 声明。

另外 - 字典没有顺序,因此您可以从查询中删除 order by 子句。

可验证的例子:

var ctrls = new List<Ctrl>
{
    new Ctrl { CtrlTypeCode = "1", Name = "My value 1" },
    new Ctrl { CtrlTypeCode = "2", Name = "My value 2" },
    new Ctrl { CtrlTypeCode = "3", Name = "My value 3" },
};
Dictionary<string, Ctrl> actionControls = ctrls.ToDictionary(ctrl => ctrl.Name);

【讨论】:

    【解决方案2】:
        public class Class2
        {
            public string name;
            public int age;
        }
    
        List<Class2> list = new List<Class2>();
        Class2 c1 = new Class2();
        c1.name = "Tom";
        c1.age = 15;
        Class2 c2 = new Class2();
        c2.name = "Ada";
        c2.age = 17;
        list.Add(c1);
        list.Add(c2);
        Dictionary<string, Class2> dict = new Dictionary<string, Class2>();
        dict = list.ToDictionary(b => b.name);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-23
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 2015-07-23
      相关资源
      最近更新 更多