I am attempting to compare datatables with two columns in an array of datatables and send the unique sets to a separate table for viewing. Is there an existing method to do this or do I need to write my own? I looked at IEqualityComparer; it appears to be only for comparing rows within a table. Furthermore, I was unable to get the thing to work, some build error about interfacing.
Anyway, I have a "master table" of the form
valueX valueY
1 10
1 12
2 10
2 13
3 10
3 12
that I sparate into an array of 3 datatable of the form
dt[1]
1 10
1 12
dt[2]
2 10
2 13
dt[3]
3 10
3 12
Upon comparison there would be two unique tables that I would like to send to dtUnique that would look like
1 10
1 12
2 10
2 13
Suggestions?
Loading
theLizardPosted Mar 11, 2010, 4:55 PM
I would not laugh because what you have learnt will help you develop bigger better code.
:)
Rodney JohnsonPosted Mar 11, 2010, 3:54 PM
created i datatables, one for each of the i shiftsets. That is the first column in the above example.
ran a double for loop (similar to a sort) with a conditional between the two loops. The conditional counted the number of rows. If the row count was not the same the sets were not the same. If the row count was the same each row for the two current sets compared. If the rows were the same a "check array" or flag was set to 1. The check array was the same length as the row count of the sets being compared. I then summed the check array. If it equaled the row count that shiftset was set to 0, another flag. The number of zeros in the "shiftSetID" array was counted and a new "uniqueShiftSetID" array of lengh shiftSetID - number of zeros was created. The nonzero values were passed to this array and then the sets in the table were selected from the datatable. Three methods were used to complete this task.
Don't laugh too hard. It works.
Thanks for all of your help.
Rod
Sam HobbsPosted Mar 9, 2010, 3:55 AM
I have not thought about all the detaisl, but I think that can work.
Amit ChoudharyPosted Mar 9, 2010, 1:07 AM
Below function takes two parameter of type datatable and return you datatable.
public static DataTable CompareTwoDataTable(DataTable dt1, DataTable dt2)
{
dt1.Merge(dt2);
DataTable d3 = dt2.GetChanges();
return d3;
}
This Method is used to compare 2 dataTable and return the difference in 3rd datatable. In this example we are going to use GetChanges method of dataTable which will give us the difference rows.Hope it helps you.
Please mark as answer if it helps you.