views:

61

answers:

3

I want to verify that 2 datatables contains same data (for unit testing), but unfortunately, Assert.AreEqual does not work, it seems that each datatable contains unique metadata that makes their references not equal.

How can I do this?

+3  A: 

If you want to know if they are different:

I would sort the DataTables by their key, then iterate over both at the same time. If the keys are different for the same position, then you know the tables are different. If the keys are the same, compare the other columns of the tables for this row.

If you want to get the differences between them:

You may also want to have a look at the Merge method of the DataTable.

Johann Blais
+1 for mentioning the Merge method. Combining it with DataTable.GetChanges and checking if there are any rows will do the trick of telling if there were any differences and what they where. Perf of merge is probably worse than a simple loop/Linq query though.
PHeiberg
A: 

Given that you are talking about SQL server and you have 2 databases on the same server you can run a cross-database EXCEPT per table to get a difference count like so :

SELECT COUNT(*) 
FROM (
    SELECT * FROM database1.dbo.table1
    EXCEPT
    SELECT * FROM database2.dbo.table1
) AS diff

I believe that is the fastest way but then again I made a lot of presumptions about your setup :)

Diadistis
This is T-SQL. He's talking about DataTables (ADO.NET)
RPM1984
@RPM1984: Indeed, I focused on the diff problem and missed that. Still it's a very good and fast alternative so I'll leave it here as reference.
Diadistis
Fair enough. Although this is for a unit-test. If he was calling the db, it would cease to be a unit-test. :)
RPM1984
A: 

How about Linq2DataSet?

PHeiberg