todictionary用法 - c#遍历




如何将数组转换为字典 (2)

如何将下面的fieldList数组转换为字典作为Dictionary<string,column> (ie 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;}
}

你可以使用linq lambda来做这个。

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

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

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

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




c#-6.0