如何将数组转换为字典

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