A. Abramov
A. Abramov

Reputation: 1865

Dataset Conversion in C#

I have a dataset with a single datatable that goes as follows:

...
<description>"abcd"</description>
<date>"1/1/2001"</date>
... 
<description>"1234"</description>
<date>"1/1/2001"</date>
...
<description>"abcd"</description>
<date>"1/1/2001"</date>
...

I want to construct a new dataset, that contains the same values, rows and columns but only where the description is "abcd", per se.

i.e, for the example values I gave, I'd like to get:

...
<description>"abcd"</description>
<date>"1/1/2001"</date>
... 
<description>"abcd"</description>
<date>"1/1/2001"</date>
...

I've seen the DataTable.Select() method, but I'm not sure if it's a good way or can work at all for what I'm trying to do.

What's the best practice to do such a thing?

Upvotes: 1

Views: 74

Answers (1)

Steve
Steve

Reputation: 216293

The Select method extracts an array of DataRow from the DataTable to which it is applied.

DataRow[] rows = ds.Tables[0].Select("description = 'abcd'");

Having the rows array declared you could check if there is something returned by the Select method and convert the array to a DataTable using the IEnumerable extension called CopyToDataTable

DataTable foundData = null;
if(rows != null)
    foundData = rows.CopyToDataTable();

Notice that the DataSet is not involved in this process but it is just the container of your original table and the new table returned by CopyToDataTable is not included in the Tables collection of the DataSet.

Upvotes: 1

Related Questions