AKA Marketing.com Logo            VISIT THE BLOG            

Blogged thoughts, is our web blog. Expect views, opinion, rants and tirades about everything and anything 

« Home / Forums »        

Subscribe to our SEO / IT related blog by entering your email address below

Blogged thoughts

| by the www.akamarketing.com team



Filtering & sorting DataTables with DataTable.Select() in C#

We all know that connections to databases from within our applications cost us processor cycles and thus time so it’s a good idea to only ‘talk’ to your database when you really need too. In the .NET framework items like DataSets and DataTables etc. facilitate this to a large degree. In this post let’s take the .Select() method of the DataTable class as evidence of this.

.NET framework 2.0 DataTable.Select() method
The DataTable.Select() method has four overloads and allows you to filter and display your rows in a number of ways in a manner similar to the way SQL does. The method can accept an expression string which although not nearly as powerful as full blown SQL does provide the advantage of not having to go back to the database to do simple things like showing records relevant to only a certain date range or a certain customer, product etc.

Below is some of the valid expressions and filters you can pop into the Select() method to narrow your record set.

Standard Operator based expressions
Things like >, <, >=, <=, =, <> are all supported of course. If your DataTable was called salesStaff and you only wanted to return staff with sales of greater than 100, you could do this with salesStaff.Select(”sales > 100″). An array of DataRows will be returned, so depending on what you want to do with the results of the Select() method you may have to copy them back into another DataTable… more on that below.

More SQL query like expressions
Filtering by Like is also supported by the Select() method which I think is quite sweet. Wildcards as in SQL are * and %. Just like most variations of SQL support aggregate types so too does the Select() expression ‘language’. Items like AVG, MIN, MAX, SUM and COUNT are all supported, so too are functions like LEN and SUBSTRING. If you need to test multple columns/conditions you can join them with the AND or OR operators but be careful if your using .Net 1.1 SP 1 as there was a documented bug in .Select() when it was used with AND.

Sorting with DataTable.Select()
This is probabely the main way I use the Select() method. Its supported not via the expression parameter but via the sort parameter which is available in two overloaded .Select() methods. Its format is columnName asc/desc.

Importing DataTable.Select() method results into another DataTable
Putting the results from a Select() query into another DataTable is often a requirement say for instance if you wanted to bind your results to a control. As mentioned above the Select() method returns an array of DataRows and since DataRows are the main building blocks of DataTables the process is very easy. Steps outlined are:

Issue .Clone() your source DataTable to create a new DataTable with the same schema
Issue .Select() on your source DataTable() to get an array of rows with the data you want
Loop through the DataRows array and issue .ImportRow() on your destination table each interation

and a code snippet to do the above might look something like the following:

//copy the schema of source table
DataTable above24 = dt.Clone();
 
//get only the rows you want
DataRow[] results = dt.Select(”age > 24″);

//populate new destination table
foreach (DataRow dr in results)
above24.ImportRow(dr);

DataTable.Select() shortcomings
The main things I don’t like about the .Select() method is how you have to go through intermediary steps to get your results into another DataTable, why can’t it just return another DataTable (which is directly bindable to a load of .NET data controls) and its lack of support for selecting distinct/unique rows which is often needed. As as note on that last one, it is possible to return distinct rows in a DataTable using LINQ which is a .NET 3.5 component, however that topic might best be served with another post at a later date.

Can’t I just use a DataView instead of calling DataTable.Select()?
You can and DataViews are directly bindable to many controls too, however it is not always the best solution due to the generally accepted believe among many developers that .Select() is much faster than using the DataView equivalent of RowFilter (property). I regularly interchange between the two for a lot of small database projects, however for the projects where I need to be processing a mega amount of data I pretty much stick with .Select() 100% of the time as I reckon it provides real speed (as in seconds, not PC invisible micro time) advantages compared to DataViews.

5 Comments on “Filtering & sorting DataTables with DataTable.Select() in C#”
1| Beardo said,

In our particular case with a datatable of 75K rows, the using a dataview with a complex filter was nearly twice as fast as the equivalent select.

2| Maharajan said,

In my case, I use a datatable with 2k rows. And when using DataView and then binding it to a control i find a lag in the binding of data. Can you please help what to do.

3| Marcin said,

Just in case some need it but don’t know about that: adding a primary keys for datatable speed SELECT() very, very much (in my case: from 60 sec. without it to less than 1 sec. with it).
The code is like following:

DataColumn[] keys = new DataColumn[3];
keys[0] = dtToExport.Columns[0];
keys[1] = dtToExport.Columns[1];
keys[2] = dtToExport.Columns[3];
dtToExport.PrimaryKey = keys;

4| Mahesh said,

Excellent Article! This is exactly what I was looking for. I still have an issue with this approach.
When I use RowFilter on the DataView derived from source DataTable, update/delete performed on the rows in DataView are affected on source DataTable.
With the approach in the article, how can I achieve the same result i.e., update/delete on source DataTable.

Any help would be very much appreciated.

5| Aze said,

Thanks man, really worked for me :)

Leave a Comment
Name:
Email:
Website:
 
HOME | ABOUT US | CONTACT US | SITEMAP | GOOGLE SITE SEARCH | TOP
12 Lorcan Crescent, Santry, Dublin 9, Ireland +353 87 9807629