如何将数组转换为字典
How to convert array to dictionary
如何将下面的 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;}
}
fieldList.ToDictionary(c=>c.name, c=>c);
您可以为此使用 linq lambda。
column[] columns = getColumninfo();
columns.ToDictionary(x => x.name, y => y);
通过添加 StringComparer.OrdinalIgnoreCase
可以确保列名查找不区分大小写。
columns.ToDictionary(x => x.name, y => y, StringComparer.OrdinalIgnoreCase);
如何将下面的 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;}
}
fieldList.ToDictionary(c=>c.name, c=>c);
您可以为此使用 linq lambda。
column[] columns = getColumninfo();
columns.ToDictionary(x => x.name, y => y);
通过添加 StringComparer.OrdinalIgnoreCase
可以确保列名查找不区分大小写。
columns.ToDictionary(x => x.name, y => y, StringComparer.OrdinalIgnoreCase);