zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    Mac下搭建SVN服务器
    iOS的扩展类,扩展属性
    关于TableViewCell高度自适应问题的整理
    关于适配的一点考虑
    Visual format language
    css命名定义
    定位之初解
    定位以及relative和absolute的结合
    float的一点想法
    javascript的学习路子
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/3847929.html
Copyright © 2011-2022 走看看