1

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;}
}
dsi
  • 3,199
  • 12
  • 59
  • 102

2 Answers2

6

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);
Igor
  • 60,821
  • 10
  • 100
  • 175
3
fieldList.ToDictionary(c=>c.name, c=>c);
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27