Databases Reference
In-Depth Information
// set display filter
string fl = "country = 'Germany'";
// set sort
string srt = "companyname asc";
The first string is a filter expression that specifies row selection criteria. It's syntacti-
cally the same as a SQL WHERE clause predicate. You want only rows where the Country
column equals “Germany”. The second string specifies your sort criteria and is syntacti-
cally the same as a SQL ORDER BY clause, giving a data column name and sort sequence.
You use a foreach loop to display the rows selected from the data table, passing the
filter and sort strings to the Select method of the data table. This particular data table is
the one named Customers in the data table collection.
// display filtered and sorted data
foreach (DataRow row in dtc["customers"].Select(fl, srt))
{
Console.WriteLine(
"{0}\t{1}",
row["CompanyName"].ToString().PadRight(25),
row["ContactName"]);
}
You obtain a reference to a single data table from the data table collection (the dtc
object) using the table name that you specify when creating the dataset. The overloaded
Select method does an internal search on the data table, filters out rows not satisfying
the selection criterion, sorts the result as prescribed, and finally returns an array of data
rows. You access each column in the row, using the column name in the indexer.
It's important to note that you can achieve the same result—much more efficiently—
if you simply use a different query for the customer data.
select
*
from
customers
where
country = 'Germany'
order by
companyname
Search WWH ::




Custom Search