Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is Dictionary.
C#
Dictionary<string, List<string>> Dic = new Dictionary<string, List<string>>();


I want to do so - i clicking button and the first Dic key with her values copied to List<string>. Click again - next Dic Key and her values and etc..
Posted
Updated 6-Sep-12 20:37pm
v2
Comments
lukeer 7-Sep-12 3:18am    
Problem with this is: There is no "first" element in a dictionary.

There are elements and you can distinguish them from one another by their Key. So you would first have to provide a method of sorting, I guess.

Do as per below

for framework 2.0

foreach ( var item in dicNumber)  {     
  listnumber.Add (item.Key);   
} 


using LINQ


var Listname= dictionary.Keys.ToList(); 


check if tis solve your problem ,thanks
 
Share this answer
 
Comments
[no name] 7-Sep-12 3:09am    
I want to copy only happened when you press the button. In your case, all the data will be copied.
I have an idea how to get them all at once:
C#
public static void Example()
{
    Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
    dic.Add(
        "first",
        new List<string>(new string[] { "first first", "second first" })
    );
    dic.Add(
        "second",
        new List<string>(new string[] {
            "first second", "second second", "third second"
        })
    );

    List<string> output = new List<string>();

    foreach (string key in dic.Keys)
    {
        output.AddRange(dic[key]);
    }
}

For a one-by-one solution, you would need to specify for each button click, which key you meant. Try a System.Windows.Forms.ListBox for that.

However, the resulting list will contain keys and values alongside. Unless the keys are formatted in some special way, you won't be able to distinguish one from another anymore.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900