468,251 Members | 1,365 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,251 developers. It's quick & easy.

Compare Datasets

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

Nov 17 '05 #1
2 7392
gi*****@gmail.com wrote:
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.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Nov 17 '05 #2
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.

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by codejockey | last post: by
reply views Thread by codejockey | last post: by
1 post views Thread by Mark | last post: by
4 posts views Thread by Justin Emlay | last post: by
reply views Thread by Frank | last post: by
reply views Thread by austenr | last post: by
6 posts views Thread by Al | last post: by
12 posts views Thread by BillE | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.