I am doing an except of two DataTables in c#. I've overloaded the IEqualityComparer
public bool Equals(T sourceRec, T targetRec)
{
string srcHash, tgtHash;
srcHash = HashGenerator.GenerateHash<T>(sourceRec);
tgtHash = HashGenerator.GenerateHash<T>(targetRec);
return srcHash.Equals(tgtHash);
}
public int GetHashCode(T obj)
{
return base.GetHashCode();
}
and except will work by taking into consideration of the Hash value of each DataRow.
public class CompareResult<T>
{
public IEnumerable<T> Inserted_Updated;
public IEnumerable<T> Updated;
public IEnumerable<T> Deleted;
}
public CompareResult<DataRow> Compare(DataTable source, DataTable target)
{
CompareResult<DataRow> rest = new CompareResult<DataRow>();
rest.Inserted_Updated = target.AsEnumerable().Except(source.AsEnumerable(), RowHashComparer);
rest.Deleted = (source.AsEnumerable().Except(target.AsEnumerable(), RowHashComparer)).Except(target.AsEnumerable().Except(source.AsEnumerable(), RowHashComparer));
return rest;
}
CompareResult<DataRow> strResult = SD1.Compare(src, tgt);
RowHashComparer is as a property used to perform custom except by comparing Hash codes. What i tracked down is the equality comparison is actually happening after the Except method has returned.
Please suggest.