Getting dictionary <string, string> from list <Control>

I want a dictionary containing the names and text of all controls. Is this possible with the predefined LINQ / colection structure / initializer methods or do I have to create a loop and add all the entries myself?

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

      

+2


a source to share


3 answers


The call should look like this:



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

      

+3


a source


It should probably look something like this:



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

      

+2


a source


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

      

+1


a source







All Articles