Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Dictionary
This is Dictionary.
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. Click again - next Dic Key and her values and etc..
Posted 6-Sep-12 21:36pm
Edited 6-Sep-12 21:37pm
v2
Comments
lukeer at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Radzhab at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I have an idea how to get them all at once:
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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 7 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100