[英]C# Convert a List to a Dictionary
I'm trying to convert a List<Ctrl>
to a Dictionary<string, Ctrl>
using Ctrl.Name
as the key.我正在尝试使用Ctrl.Name
作为键将List<Ctrl>
转换为Dictionary<string, Ctrl>
。 I've referenced this page to attempt to achieve this: Microsoft's Enumerable.ToDictionary Method page我已经参考了这个页面来尝试实现这一目标: Microsoft's Enumerable.ToDictionary Method page
Hence, my code looks like this:因此,我的代码如下所示:
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);
I get an error on ToDictionary<string, Ctrl>(a => a, b => b.Name)
which reads: Instance argument: cannot convert from 'System.Collections.Generic.List<AppData.Ctrl>' to 'System.Collections.Generic.IEnumerable'我在ToDictionary<string, Ctrl>(a => a, b => b.Name)
上收到错误ToDictionary<string, Ctrl>(a => a, b => b.Name)
:实例参数:无法从 'System.Collections.Generic.List<AppData.Ctrl>' 转换为 'System .Collections.Generic.IEnumerable'
Argument 3: cannot convert from 'lambda expression' to 'System.Collections.Generic.IEqualityComparer<AppData.Ctrl>'参数 3:无法从“lambda 表达式”转换为“System.Collections.Generic.IEqualityComparer<AppData.Ctrl>”
Argument 2: cannot convert from 'lambda expression' to 'System.Func<string,AppData.Ctrl>'参数 2:无法从“lambda 表达式”转换为“System.Func<string,AppData.Ctrl>”
'System.Collections.Generic.List<AppData.Ctrl>' does not contain a definition for 'ToDictionary' and the best extension method overload 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable, System.Func<TSource,TKey>, System.Collections.Generic.IEqualityComparer)' has some invalid arguments '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)' 有一些无效的参数
Not entirely sure what I'm doing wrong here.不完全确定我在这里做错了什么。 Also, it seems strange to me that a Dictionary is declared in the order <key, value>
and the ToDictionary constructor takes the lambda functions in the order <value, key>
.此外,我觉得字典以<key, value>
的顺序声明,而 ToDictionary 构造函数以<value, key>
的顺序采用 lambda 函数,这对我来说似乎很奇怪。
How do I fix this?我该如何解决? I'm a bit lost.我有点失落。 Thanks.谢谢。
Edit: Added to list, and included all the compiletime errors.编辑:添加到列表中,并包含所有编译时错误。
Edit 2: Removed the type arguments for ToDictionary.编辑 2:删除了 ToDictionary 的类型参数。 I've also removed the ToList() call which is superflous, so that the code reads:我还删除了多余的 ToList() 调用,因此代码如下:
oActionControls = (from ctrl in _ctrls.Items
where ctrl.CtrlTypeCode == "BUTTON"
orderby ctrl.TabIndex
select ctrl).ToDictionary(a => a.Name);
which works correctly.哪个工作正常。
Assuming you have a List<Ctrl>
, you could create the dictionary simply by calling .ToDictionary()
with a key selector parameter (see MS docs ):假设您有一个List<Ctrl>
,您可以通过使用键选择器参数调用.ToDictionary()
来创建字典(请参阅MS 文档):
Dictionary<string, Ctrl> actionControls = ctrls.ToDictionary(ctrl => ctrl.Name);
Make sure you have a using System.Linq;
确保你有一个using System.Linq;
statement.陈述。
Also - a dictionary does not have an order, so you could remove the order by
clause from the query.此外 - 字典没有顺序,因此您可以从查询中删除order by
子句。
Verifyable example:可验证的例子:
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);
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.