C# code to filter a datatable

Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 587
Let's say we have a DataTable in which their is a column of age. We need to select the person whose age is the older one. The below code will do

public DataTable GetData()


DataTable dt = new DataTable();

//Add some columns

dt.Columns.Add(new DataColumn("Age", typeof(Int32)));

//Add some rows

//First Row

DataRow row1 = dt.NewRow();

row1("Age") = 21;

//Second Row

DataRow row2 = dt.NewRow();

row2("Age") = 11;

//Third Row

DataRow row3 = dt.NewRow();

row3("Age") = 12;

//Fourth Row

DataRow row4 = dt.NewRow();

row4("Age") = 55;

//Fifth Row

DataRow row5 = dt.NewRow();

row5("Age") = 18;

//Add the rows to the table






//return the table

return dt;


Then filter as under

var oldestRecord = GetData().AsEnumerable().Select(dr=>dr["Age"]).Max();

Comments or Responses

Login to post response