【问题标题】:Getting Dictionary<string,string> from List<Control>从 List<Control> 获取 Dictionary<string,string>
【发布时间】:2010-04-13 11:09:47
【问题描述】:

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

这给了我一条错误消息:

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

【问题讨论】:

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


    【解决方案1】:

    调用应该是这样的:

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

    【讨论】:

      【解决方案2】:

      它应该看起来像这样:

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

      【讨论】:

        【解决方案3】:
        Dictionary<String, String> myControls = ((from Control c in Controls select c).ToDictionary(c => c.Name, c => c.Text))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多