Вы находитесь на странице: 1из 1

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 its 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 and allow you to update date with too much pain. In this post lets 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 dont like about the .Select() method is how you have to go through intermediary steps to get your results into another DataTable, why cant 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. Cant 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.

Вам также может понравиться