I wanted to verify, are DataTables in DataSets the same. I found a few similar implementations on StackOverflow, but the one that I've selected
tbl1.Rows[i][c] 2 object {long}
tbl2.Rows[i][c] 2 object {long}
tbl1.Rows[i][c] == tbl2.Rows[i][c] false
I found, that it should be used Equals instead of ==.
Equals(tbl1.Rows[i][c], tbl2.Rows[i][c]) true
There are a few articles, explaining the difference and reasons behind it.
Below are tested methods to check, are DataTables or DataSets the same
/// <summary>
///
/// </summary>
/// <param name="tbl1"></param>
/// <param name="tbl2"></param>
/// <returns></returns>
public static bool AreTablesTheSame( DataTable tbl1, DataTable tbl2)
{
if (tbl1.Rows.Count != tbl2.Rows.Count || tbl1.Columns.Count != tbl2.Columns.Count)
return false;
for ( int i = 0; i < tbl1.Rows.Count; i++)
{
for ( int c = 0; c < tbl1.Columns.Count; c++)
{
if (!Equals(tbl1.Rows[i][c] ,tbl2.Rows[i][c]))
return false;
}
}
return true;
}
/// <summary>
/// Compare content of all rows in the table.
/// </summary>
/// <param name="ds1"> The DS1.</param>
/// <param name="ds2"> The DS2.</param>
/// <returns></returns>
public static bool AreTablesTheSame( DataSet ds1, DataSet ds2)
{
if (ds1 == null && ds2 == null)
return true;
if (ds1 == null || ds2 == null) //only one is null
return false;
if (ds1.Tables.Count != ds2.Tables.Count)
return false;
for ( int i = 0; i < ds1.Tables.Count; i++)
{
if (! DataTableHelper.AreTablesTheSame(ds1.Tables[i] ,ds2.Tables[i]))
return false;
}
return true;
}