Searching Data in DataTable in .Net

Searching Data in DataTable in .Net

Added by Nasir Mahmood updated on Friday, November 17, 2017

Problem:

Searching data in a DataTable is one of the most common user interaction and there are number of ways by which we can filter data in .Net. In this tutorial we will discuss two popular techniques for filtering data as following

  1. Using DataTable.Select()
  2. Using Linq to DataSet

Generate Data:

For this tutorial we required some dummy data so that we can understand what's going on.

data table columns

_dataTable.Columns.Add("ID", typeof(int));
_dataTable.Columns.Add("Name", typeof(string));
_dataTable.Columns.Add("Address", typeof(string));
_dataTable.Columns.Add("Class", typeof(int));
DataRow dr = _dataTable.NewRow();
dr["ID"] = 1;
dr["NAME"] = "Name 1";
dr["Address"] = "Address 1";
dr["Class"] = 5;
_dataTable.Rows.Add(dr);
dr = _dataTable.NewRow();
dr["ID"] = 2;
dr["NAME"] = "Name 2";
dr["Address"] = "Address 2";
dr["Class"] = 5;
_dataTable.Rows.Add(dr);
dr = _dataTable.NewRow();
dr["ID"] = 3;
dr["NAME"] = "Name 3";
dr["Address"] = "Address 3";
dr["Class"] = 6;
_dataTable.Rows.Add(dr);
dr = _dataTable.NewRow();
dr["ID"] = 4;
dr["NAME"] = "Name 4";
dr["Address"] = "Address 4";
dr["Class"] = 6;
_dataTable.Rows.Add(dr);
dr = _dataTable.NewRow();
dr["ID"] = 5;
dr["NAME"] = "Name 4";
dr["Address"] = "Address 3";
dr["Class"] = 7;
_dataTable.Rows.Add(dr);			
			

Now we have dummy data for our tutorial, its time to apply filter on this data

adding rows to data table

Filtering Data:

Filter by ID using DataTable.Select
By using following code we can filter data by ID column, the number of rows returned by this code is 1.

DataRow[] filter_rows = _dataTable.Select("ID=3");
int count = filter_rows.Count();			
			

Filter by ID using Linq
By using following code we can filter data by ID column, and the number of rows returned by this code is 1.

var filter_rows_linq = _dataTable.AsEnumerable().Where(row => row.Field("ID") == 3);
count = filter_rows_linq.Count();			
			

Filter by Name using DataTable.Select
By using following code we can filter data by Name column, and the number of rows returned by this code is 2 because we have 2 rows which has same Name value.

filter_rows = _dataTable.Select("Name='Name 4'");
count = filter_rows.Count();			
			

Filter by Name using Linq
By using following code we can filter data by Name column, and the number of rows returned by this code is 2 because we have 2 rows which has same Name value.

filter_rows_linq = _dataTable.AsEnumerable().Where(row => row.Field("Name") == "Name 4");
count = filter_rows_linq.Count();			
			

Filter by Name and Address using DataTable.Select
By using following code we can filter data by Name and Address columns, and the number of rows returned by this code is 1 because we have 2 rows which has same Name value but 1 row which has same Address so result will be 1.

filter_rows = _dataTable.Select("Name='Name 4' AND Address='Address 4'");
count = filter_rows.Count();			
			

Filter by Name and Address using Linq
By using following code we can filter data by Name and Address columns, and the number of rows returned by this code is 1 because we have 2 rows which has same Name value but 1 row which has same Address so result will be 1.

filter_rows_linq = _dataTable.AsEnumerable().Where(row => row.Field("Name") == "Name 4" 
											 && row.Field("Address")=="Address 4");
count = filter_rows_linq.Count();			
			

Related Tags

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected