flavour404
flavour404

Reputation: 6312

How to query a DataSet and iterate through the result?

I have a DataSet which contains two tables, Publication and Owner, which are linked on Publication ID. How do I query the dataset? What I am trying to do is get all of the owners for a particular publication, and then I want to iterate over the resulting set, concatenate the owner names together and populate a label with the information...

But lets begin with, how do i query the dataset?

I also have a DataRelation, can I query that somehow to get the child rows for the current row?

Thanks.

Upvotes: 5

Views: 7009

Answers (2)

Mitch Wheat
Mitch Wheat

Reputation: 300759

ADO.NET supports two fundamental approaches for performing filtering and sorting of datasets:

The DataTable Select Method - This method is overloaded to accept arguments to filter and sort data rows returning an array of DataRow objects.

The DataView object sort, filter and find methods - This object uses the same filter arguments supported by the Select method, but the DataView exposes structures that can be bound to data-aware controls. See DataView.RowFilter

Iterating over filtered rows is as easy as:

DataTable dt;
...
foreach (DataRow dr in dt.Select(filter))
{
    // ...
}

This article contains several examples: A Practical Guide to .NET DataTables, DataSets and DataGrids - Part 1

Upvotes: 6

tbreffni
tbreffni

Reputation: 5130

You could look into LINQ to Dataset, which allows you to perform queries against DataSets with multiple tables. You can perform joins between the tables on the appropriate columns amongst other things.

Upvotes: 3

Related Questions