我有一本像这样的字典:
Dictionary<string, string> myDict = new Dictionary<string, string>
{
{ "key1", "value1,value2,value3" },
{ "key2", "value4,value5" }
}
如何转换为具有以下性质的 KeyValuePair
的 List
:
List<KeyValuePair<string, string>> myList = n List<KeyValuePair<string, string>>
{
{ "key1", "value1" },
{ "key1", "value2" },
{ "key1", "value3" },
{ "key2", "value4" },
{ "key2", "value5" }
}
很抱歉问题主题可能不清楚。
最佳答案
这个转换的关键是SelectMany
方法:
var res = myDict
.SelectMany(p => p.Value.Split(',').Select(s => new KeyValuePair<string, string>(p.Key, s)))
.ToList();
关于c# - 如何将包含逗号分隔字符串值的字典转换为键值对集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40069986/