c#的dictionary转list
可以使用LINQ的ToList()方法将C#中的Dictionary转换为List,示例代码如下:
using System.Collections.Generic;
using System.Linq;// 创建Dictionary
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 1);
dict.Add("banana", 2);
dict.Add("orange", 3);// 转换为List
List<KeyValuePair<string, int>> list = dict.ToList();// 遍历输出List中的元素
foreach (KeyValuePair<string, int> kvp in list)
{Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
其中,ToList()方法将Dictionary转换为List
// 获取键的列表
List<string> keyList = dict.Select(x => x.Key).ToList();// 获取值的列表
List<int> valueList = dict.Select(x => x.Value).ToList();
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
