Compare Datasets

  • Thread starter Thread starter gibster
  • Start date Start date
G

gibster

Hi
I'm looking for some help comparing 2 datasets,
each dataset has 2 datatables;

the first table's rows consist of

text
status (1 or 0)
GUID

The GUID and the text will always be in sync in each version of the
dataset, but the status could be different.

The second table in each dataset contains a list of GUID's that have
been deleted.

What I want to do is match each GUID and check if the status in either
version of the table has changed to 1, if so use this value in the
merged dataset, then check if either dataset has a new rows (based on
GUID) and add them to the merged dataset, and finally check both the
removed GUIDs lists and remove any rows that have these GUIDS in the
merged datatable.

I'm fairly new to c# and .net and i'm struggling with this, so any help
would be appreciated.
Thanks in advance
Charlie
 
each dataset has 2 datatables; the first table's rows consist of

text, status (1 or 0), GUID

The GUID and the text will always be in sync in each version of the
dataset, but the status could be different. The second table in each
dataset contains a list of GUID's that have been deleted.

What I want to do is match each GUID and check if the status in either
version of the table has changed to 1, if so use this value in the
merged dataset, then check if either dataset has a new rows (based on
GUID) and add them to the merged dataset, and finally check both the
removed GUIDs lists and remove any rows that have these GUIDS in the
merged datatable.

Unfortunately, I don't think DataSet.Merge() is flexible enough to support
the behaviour you desire. If these tables were in a real database, you
could achieve something like this with an outer join followed by a DELETE
statement. As it is, however, you can still use the primary-key foreign-key
relationship functionality to speed things up significantly.

First, create a DataSet bothSet and copy all four tables into it. Let's say
your tables are called T1 and T2 (each with text, status, GUID) and D1 and
D2 (the deleted row tables). Consider this statement:

bothSet.Relations.Add(
new DataRelation("T1_to_T2_GUID",
bothSet.Tables["T1"].Columns["GUID"],
bothSet.Tables["T2"].Columns["GUID"],
false));

After doing this, you can iterate through T1 and use the DataRow member
GetChildRows to identify any rows in T2 with the same GUID. You can examine
both and make your decision about what row to insert into your result
table. You can also identify new rows as the rows which have no child rows.
If you add the same relation with T1 and T2 reversed, you can use it to
identify new rows in T2 as well.

Finally, to perform the necessary deletions, you can play the same trick.
Create a relation between D1/D2 and your result table based on the GUID as
above. Then iterate through D1 and D2 and use GetChildRows to get the rows
you need to delete (with DataRow's Delete member).

Caveat: I have not compiled or tested any code in this mail. If anyone can
come up with a better way of doing this I invite it. I hope this helps, and
please write back if any of this wasn't clear or was unhelpful.
 
Thats a great help, thanks a lot, I'll work on this later on today, and
post my results.
I had been trying to get somewhere with dataset.merge before i posted
this, but i got the feeling it wasn't capable of doing what i wanted,
so at least i was right about that.
thanks again.
Charlie.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top