简体   繁体   English

获取字典 <string,string> 从清单 <Control>

[英]Getting Dictionary<string,string> from List<Control>

I want a dictionary containing the names and text of all controls. 我想要一个包含所有控件的名称和文本的字典。 Is it possible with predefined framework methods/LINQ/colection initializers or do I have to make a loop and add all entries by myself? 是否可以使用预定义的框架方法/ LINQ / colection初始化程序,还是我必须自己制作一个循环并添加所有条目?

This gives me an error message: 这给我一个错误信息:

List<Control> controls;
// .. initialize list ..
controls.ToDictionary((Control child,string k)=>new KeyValuePair<string,string>(child.Name, child.Text));

The call should be like this: 调用应如下所示:

controls.ToDictionary(
    c => c.Name,       // Key selector
    c => c.Text);      // Element selector.

它可能看起来像这样:

controls.ToDictionary(c => c.Name, c => c.Text);
Dictionary<String, String> myControls = ((from Control c in Controls select c).ToDictionary(c => c.Name, c => c.Text))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM