The data of
DataTable
can be converted as a
List
of
Dictionary<string,object>
where each row of the
DataTable
is represented as an element of type Dictionary in this
List
. Each column value within the
Dictionary
is referenced by the
Column name
(as given in
DataTable
), as a
key
as shown below
DataTable amounts = new DataTable();
amounts.Columns.Add("Month", typeof(string),null);
amounts.Columns.Add("Name", typeof(string),null);
amounts.Columns.Add("Amout", typeof(int),null);
amounts.Rows.Add("March","John",0);
amounts.Rows.Add("April","Ishan",25);
amounts.Rows.Add("May","Raman",50);
amounts.Rows.Add("March","Sheron",0);
amounts.Rows.Add("March","Ramesh",75);
List<dictionary<string,object>> amountsDic =
amounts.AsEnumerable().Select (dr => {
var dic = new Dictionary<string,object>();
dr.ItemArray.Aggregate(-1,(int i, object v) => {
i+=1; dic.Add(amounts.Columns[i].ColumnName,v);
return i;
});
return dic;
}).ToList();