I am using DataRelation
to compare two DataTable
and return a third table containing the rows that are not share by both original tables by using this tutorial. Below is the code.
public static DataTable Difference(DataTable First, DataTable Second)
{
//Create Empty Table
DataTable table = new DataTable("Difference");
//Must use a Dataset to make use of a DataRelation object
using(DataSet ds = new DataSet())
{
//Add tables
ds.Tables.AddRange(new DataTable[]{First.Copy(),Second.Copy()});
//Get Columns for DataRelation
DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];
for(int i = 0; i < firstcolumns.Length; i++)
{
firstcolumns[i] = ds.Tables[0].Columns[i];
}
DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];
for(int i = 0; i < secondcolumns.Length; i++)
{
secondcolumns[i] = ds.Tables[1].Columns[i];
}
//Create DataRelation
DataRelation r = new DataRelation(string.Empty,firstcolumns,secondcolumns,false);
ds.Relations.Add(r);
//Create columns for return table
for(int i = 0; i < First.Columns.Count; i++)
{
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);
}
//If First Row not in Second, Add to return table.
table.BeginLoadData();
foreach(DataRow parentrow in ds.Tables[0].Rows)
{
DataRow[] childrows = parentrow.GetChildRows(r);
if(childrows == null || childrows.Length == 0)
table.LoadDataRow(parentrow.ItemArray,true);
}
table.EndLoadData();
}
return table;
}
Surprisingly enough when I exercised this code, it only worked on few cases and failed in others.
There was one case, I was comparing two identical table (but has different source) that both has:
- Same Column Name &
DataType
- Same Number of Rows & Columns
- Same Value in Each Cells
Only to have a brand new table returned that actually has the exact same composition as the two original tables!
What did I possibly miss?
Is it possible that two identical tables (sharing features I mentioned above) can have other different properties (which doesn't seem visible to users' eyes)?
Or is it possible that this is actually a bad method? What are the possible alternatives?
EDITED
- Both tables have same primitive datatype such as:
System.String
,System.Int32
,System.DateTime
- These code doesn't work on all samples I tested
- Here is a print screen of 1 sample (using
DataSet
Visualizer)