Good evening,
I’m building a kind of cache comparing two datatable (the last read and the one I need to write) and retrieving only modified rows.
I’m using DataRowComparer.Default as Equality Comparer, but it has wrong aim comparing rows with many fields.
It works perfectly with 3 columns table with short text values, but comparing long text descriptions it fails returning the whole table even if I change a single char.
The code is very simple:
var diffDs = ds.Tables[0].AsEnumerable().Except(cachedTable.AsEnumerable(), DataRowComparer.Default);
Ideas?
Thanks!
Update:
manually debugging I was able to compare ds.Tables[0].AsEnumerable() rows vs cachedtable.AsEnumerable() rows: absolutely equals but different for DataRowComparer Except. I’ve deleted any DateTime column trying to avoid Format differences without success.
Intersection doesn’t work too.
Update 2:
Except doesn’t work with empty/null fields. They seem to be different for the IEqualityComparer.
I found a solution:
DataRowComparer code works like this:
As you can see null values and DBNull are differently considered and null==DBNull is always false. Obviously an empty string looks like null field, but it’s not DBNull.
So, two tables apparently similar are different due to this null values.
My solution parses ds.Tables[0] while constructing from xml source (this is the one with empty strings and null values) and operates a substitution with System.Convert.DBNull
Now the comparison works perfectly.
P.S. \n and \r\n are also different if you build a datatable from sql query or from xml source. Easy to solve assumed the null/DBNull solution above.