从List <control> </control> </string,string>获取Dictionary <string,string>

时间:2010-04-13 11:09:47

标签: c# .net linq c#-3.0 dictionary

我想要一个包含所有控件名称和文本的字典。是否可以使用预定义的框架方法/ LINQ / colection初始化器,或者我是否必须自行创建循环并添加所有条目?

这给了我一条错误信息:

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

3 个答案:

答案 0 :(得分:3)

电话应该是这样的:

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

答案 1 :(得分:2)

应该看起来像这样:

controls.ToDictionary(c => c.Name, c => c.Text);

答案 2 :(得分:1)

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