views:

335

answers:

1

Is there a relatively easy way to extract a relationship-consistent subset of a DataSet? The subset I want is: the rows I'm interested in, all the child and parent rows required by those rows, and nothing else.

I have around a dozen tables, each with two to four relationships to other tables.

I figure I could write code to traverse the data tables and relationships given a day or two, but I'd prefer to re-use existing code and spend that day or two on my product.

Background:

I have a strongly typed DataSet pulled from a database of the components my company sells.

I'm considering another strongly-typed DataSet to store proposed solutions. There'll be one row per item on the Bill of Materials (BOM). Each row will describe the component's configuration.

I don't want to put solution tables in the component DataSet.

When I serialize the solution DataSet via WriteXml, I'd like to persist just enough information about the components. If I'm storing primary keys from the component tables, that shouldn't be too hard.

It occurs to me that persisted solutions could survive expiry of data from the main component DataSet if I also persisted the appropriate rows from that DataSet. I don't want to persist the whole component DataSet, though, hence my question about extracting a useful subset.

+1  A: 

The easiest way I can think of is to call DataRow.SetModified() on each row and traverse through any child rows you need to get, then call the DataSet.GetChanges() method on the DataSet, which will return you only the rows you've flagged.

You would also need to call RejectChanges() in the original DataSet after calling GetChanges to use it again.

Jerome Laban

related questions