From Dictionary to List

Looks like you want to create a new List<string> based on your all string elements in the dictionary’s List values. You may use SelectMany to flatten it out and get a list using the following code:

Dictionary<string, List<string>> Dic = new Dictionary<string, List<string>>();
Dic.Add("1", new List<string>{"ABC","DEF","GHI"});
Dic.Add("2", new List<string>{"JKL","MNO","PQR"});
Dic.Add("3", new List<string>{"STU","VWX","YZ"});

List<string> strList = Dic.SelectMany(r => r.Value).ToList();

Where strList will contain all the string items from the dictionary in a single list.

Browse More Popular Posts

Leave a Comment