C # Datatable conversion Dictionary,
DataTable dt = new DataTable ();
Dt. Columns. Add ("name ");
Dt. Columns. Add ("student ID ");
Dt. Rows. Add (new object [] {"cucumber", "1 "});
Dt. Rows. Add (new object [] {"John", "2 "});
Dt. Rows. Cast <DataRow> (). ToDictionary (x => x [0]. ToString (), x => x [1]. ToString ());
Custom: (simple Edition)
Public static List <Dictionary <string, object> DataTableToDictionary (DataTable dt)
{
List <Dictionary <string, object> list = new List <Dictionary <string, object> ();
Foreach (DataRow dr in dt. Rows)
{
Dictionary <string, object> dct = new Dictionary <string, object> ();
Foreach (DataColumn column in dt. Columns)
{
Dct. Add (column. ColumnName, dr [column. ColumnName]. ToString ());
}
List. Add (dct );
}
Return list;
}
Dt. Rows. Cast <DataRow> (). Select (x =>
{
Var dict = new Dictionary <string, string> ();
Dict. Add ("name", x [0]. ToString ());
Dict. Add ("student ID", x [1]. ToString ());
Return dict;
}). ToList ();