how to convert list of list of string to dictionary in c#

I’m doing a project where i need to convert a list<list> to a dictionary object i have used

var myDic = GetSomeStrings().ToDictionary(x => x, x => x.Number('A'));

to convert but it didn’t work for list<list>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try using .ToDictionary(sublist => sublist[0], sublist => sublist[1])

var list = new List<List<string>>()
{
    new List<string>() { "0", "A" },
    new List<string>() { "1", "B" }
};

var dictionary = list.ToDictionary(sublist => sublist[0], sublist => sublist[1]);

foreach (var (key, value) in dictionary)
{
    Console.WriteLine($"{key}: {value}");
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x