Data table sorting in C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 292
The following will sort the records in the datatable based on the data column age.

DataTable dt = new DataTable();
DataRow dr;
dr = dt.NewRow();
dr[0] = 21;
dr[1] = "AAA";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 20;
dr[1] = "AAA";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 22;
dr[1] = "AAA";
dt.Rows.Add(dr);

//sorting
dt.AsEnumerable().Select(dr=>dr["age"]).OrderbyDesc(o=>o).ToList();

Comments or Responses

Login to post response