一、创建List的时候,将Dictionary的Value值作为参数
Dictionary<int, Person> dic = new Dictionary<int, Person>();
List<Person> pList = new List<Person>(dic.Values);
二、用Dictionary对象自带的ToList方法
Dictionary<int, Person> dic = new Dictionary<int, Person>();
List<Person> pList=new List<Person>();
pList = dic.Values.ToList<Person>();
三、建立List,循环Dictionary逐个赋值
Dictionary<int, Person> dic = new Dictionary<int, Person>();
List<Person> pList=new List<Person>();
foreach (var item in dic)
{
pList.Add(item.Value);
}
四、创建List后,调用List.AddRange方法
Dictionary<int, Person> dic = new Dictionary<int, Person>();
List<Person> pList=new List<Person>();
pList.AddRange(dic.Values);
五、通过Linq查询,得到结果后调用ToList方法
Dictionary<int, Person> dic = new Dictionary<int, Person>();
List<Person> pList=new List<Person>();
pList = (from temp in dic select temp.Value).ToList();
文章转载自:ASP.NET中Dictionary如何转换为list http://www.studyofnet.com/news/1206.html
本文介绍了五种将C#中的Dictionary集合转换为List集合的方法,包括直接使用Dictionary的Values属性结合构造函数、ToList方法、foreach循环遍历、AddRange方法以及LINQ查询等方式。
6561

被折叠的 条评论
为什么被折叠?



