简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM