0

I have some data

List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();

And as a result i need a Dictionary of dictionaries like

Dictionary<string, Dictionary<string, string>> data2 = new Dictionary<string, Dictionary<string, string>>();

How can I convert this?

5
  • 3
    What do you want to use as keys in the new dictionary? Commented Jul 9, 2015 at 15:51
  • as a key i want to use 1 value on each Item from data Commented Jul 9, 2015 at 15:55
  • 1
    Show some sample data would help. Commented Jul 9, 2015 at 15:59
  • So i found the answer, it is not preety but it works. Commented Jul 9, 2015 at 17:04
  • foreach(Dictionary<string, string> item in data) { string title = null; var dictionary = new Dictionary<string, string>(); foreach(KeyValuePair<string, string> kp in item) { if (kp.Key == "title") { title = kp.Value; } else { dictionary.Add(kp.Key, kp.Value); } } data2.Add(title, dictionary); } Commented Jul 9, 2015 at 17:06

1 Answer 1

1

You can convert a List to a Dictionary with the ToDictionary method, but you need to specify a key for each entry. For example, you can take an arbitrary value from the inner dictionary:

var data2 = data.ToDictionary(d => d.Values.First());

However, I smell an XY problem here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.