Thursday, December 1, 2016

Get selected data from datatable in c#

Example
            string StudentName = "foo";
            DataTable dt = new DataTable();
            int id = (from DataRow dr in dt.Rows
                      where (string)dr["StudentName"] == StudentName
                      select (int)dr["id"]).FirstOrDefault();

Example

            DataRow[] result = datatable.Select("age >= 18");
            foreach (DataRow row in result)
            {
                Console.WriteLine("{0}, {1}", row[0], row[1]);
            }

Example
by LINQ
var results = from myRow in myDataTable.AsEnumerable()
where myRow.Field<int>("RowNo") == 1 select myRow;

No comments:

Post a Comment