问题
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