Danish ali
Danish ali

Reputation: 17

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>

Upvotes: 0

Views: 103

Answers (1)

Sebasti&#225;n
Sebasti&#225;n

Reputation: 88

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}");
}

Upvotes: 1

Related Questions