Convert dictionary to list collection in C#

Viewed 270692

I have a problem when trying to convert a dictionary to list.

Example if I have a dictionary with template string as key and string as value. Then I wish to convert the dictionary key to list collection as a string.

Dictionary<string, string> dicNumber = new Dictionary<string, string>();
List<string> listNumber = new List<string>();

dicNumber.Add("1", "First");
dicNumber.Add("2", "Second");
dicNumber.Add("3", "Third");

// So the code may something look like this
//listNumber = dicNumber.Select(??????);
7 Answers
List<string> keys = dicNumber.Keys.ToList();
List<string> values = keys.Select(i => dicNumber[i]).ToList();

This ensures that dicNumber[keys[index]] == values[index] for each possible index.

Related