如何将数组转换为字典

时间:2017-03-20 13:02:34

标签: c# linq dictionary c#-6.0

如何将下面的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;}
}

2 个答案:

答案 0 :(得分:6)

你可以使用linq lambda。

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

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

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

答案 1 :(得分:3)

fieldList.ToDictionary(c=>c.name, c=>c);