How to convert array to dictionary

試著忘記壹切 提交于 2019-11-27 19:37:16

问题


How to convert below fieldList array to dictionary as 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;}
}

回答1:


You can use a linq lambda for this.

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

By adding StringComparer.OrdinalIgnoreCase you can ensure that the column name lookup is case insensitive.

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



回答2:


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


来源:https://stackoverflow.com/questions/42904329/how-to-convert-array-to-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!