Dictionary CovertTo List

简介:



示例代码

假设有如下一个Dictionary 要转换成List 
Dictionary<string, string> dicNumber = new Dictionary<string, string>();
    List<string> listNumber = new List<string>();

    dicNumber.Add("a", "First");
    dicNumber.Add("b", "Second");
    dicNumber.Add("c", "Third");

Enumerable.Select<TSource, TResult> 方法 (IEnumerable<TSource>, Func<TSource,TResult>)

将序列中的每个元素投影到新表中。 (由 Enumerable 定义。)

有如下几种方法:

方法1

listNumber=dicNumber.Select(kvp=>kvp.Key).ToList()

上面代码中:kvp=>kvp.Key 将Dictionary中的每个元素投影到新表中,Func并返回TResult,然后把结果转成List

 

方法2

listNumber=dicNumber.Keys.ToList();

Dictionary.Keys 获取包含 Dictionary<TKey, TValue> 中的键的集合

 

方法3

foreach(var item in dicNumber)

{

listNumber.Add(item.key);

}

 

方法4

var keys=new List<string>(dicNumber.Keys);

本文转自赵青青博客园博客,原文链接:http://www.cnblogs.com/zhaoqingqing/p/3847929.html,如需转载请自行联系原作者


相关文章
|
3月前
|
存储 安全 编译器
Python学习日记(一:List、Tuple、dictionary)
1.列表、元组和字典都是序列 2.列表字典可以修改和删除序列中的某个元素,而元组就是一个整体,不能修改和删除,一定要修改或删除的话,只能修改和删除整个元组。 3.既然元组不能删除和修改,有什么作用呢? 1.元组比列表遍历速度快,因为元组是一个整体,运算效率高; 2.正是因为不能修改,元组可以保护不需要修改的数据,可以使代码结构更安全。
C#List与ArrayList,Hashtable与Dictionary总结
C#List与ArrayList,Hashtable与Dictionary总结
49 0
C#List与ArrayList,Hashtable与Dictionary总结
|
存储 算法 索引
倒排索引的数据结构:Term index、Term Dictionary、Posting List
倒排索引的数据结构:Term index、Term Dictionary、Posting List
倒排索引的数据结构:Term index、Term Dictionary、Posting List
|
C# 存储
C#数组,List,Dictionary的相互转换
本篇文章会向大家实例讲述以下内容:  将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dictionary转换为List 首先这里定义了一个“Student”的类,它有三个自动实现属性。
1225 0
|
Python
python遍历 truple list dictionary的几种方法
def TestDic1(): dict2 ={'aa':222,11:222} for val in dict2: print val def TestDic2(): dict2 ={'aa':222,11:222} for (key,val) in dict2.
859 0
|
程序员 C++ Python
代码练习 简单文件读写 字符串 数组的处理 list Dictionary
func.py # -*- coding: GBK -*- """ 在Python中默认是 Ansi编码格式 要使用中文需要 明确指定编码 数组分为动态数组和静态数组 动态数组可以动态添加 元素 静态数组不能改变 数据结构 def 定义function def Func: ...
879 0
|
开发框架 安全 .NET
C#2.0泛型--Dictionary,List用法
泛型是 C#2.0 语言和公共语言运行库 (CLR) 中的一个新功能。泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的指定推迟到客户端代码声明并实例化该类或方法的时候。
783 0
|
5月前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
65 3