【问题标题】:How to convert array to dictionary如何将数组转换为字典
【发布时间】:2017-08-11 18:34:46
【问题描述】:

如何将下面的 fieldList 数组转换为 Dictionary<string,column> (i.e. Dictionary of name property of column class as key and value property of dictionary as column object) 的字典。

public class columninfo
{
 public string name {get;set;}
 public column[] fieldList {get;set;}
}

public class column
{
  public string name {get;set;}
  public string fieldname {get;set}
  public string format {get;set;}
}

【问题讨论】:

    标签: c# linq dictionary c#-6.0


    【解决方案1】:
    fieldList.ToDictionary(c=>c.name, c=>c);
    

    【讨论】:

    • @TimSchmelter 你在讲父类的名字,他问了里面的数组
    【解决方案2】:

    您可以为此使用 linq lambda。

    column[] columns = getColumninfo();
    columns.ToDictionary(x => x.name, y => y);
    

    通过添加StringComparer.OrdinalIgnoreCase,您可以确保列名查找不区分大小写。

    columns.ToDictionary(x => x.name, y => y, StringComparer.OrdinalIgnoreCase);
    

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 1970-01-01
      • 2016-03-11
      • 2017-01-19
      • 2017-07-21
      • 2021-08-10
      • 1970-01-01
      • 2017-10-10
      相关资源
      最近更新 更多