开发者

Getting Dictionary<string,string> from List<Control>

开发者 https://www.devze.com 2022-12-26 10:10 出处:网络
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 mysel

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?

This gives me an error message:

List<Control> controls;
// .. initialize list ..
controls.ToDictionary((Control child,string k)=>new K开发者_如何学运维eyValuePair<string,string>(child.Name, child.Text));


The call should be like this:

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


It should probably look something like this:

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))
0

精彩评论

暂无评论...
验证码 换一张
取 消