I'm trying to use LINQ to compare columns in 2 DataTables for differences. The two columns are of the same type (Int32) and the query for both is the same, but only one gives the expected results. The other one does not always catch changes. It registers changes in value from 1 to 0, but changes in value from 0 to 1 are ignored. The two LINQ statements are shown below.
This works:
Dim result = From table1 In newTable
Where Not (From table2 In origTable
Where (table2("Ack Required") = table1("Ack Required"))).Any()
Select table1.ItemArray()
This works when the value changes to 1, but does nothing when it changes to 0:
Dim result2 = From table1 In newTable
Where Not (From table2 In origTable
Where (table2("Active") = table1("Active"))).Any()
Select table1.ItemArray()
What am I doing wrong here?
Edit: Here is a looping implementation I found that does what I'm trying to do:
Public Function AreTablesTheSame(table1 As DataTable, table2 As DataTable) As Boolean
' Reference - https://stackoverflow.com/questions/7517968/how-to-compare-2-datatables
' If row/column count are different there is no need to look at data
If table1.Rows.Count <> table2.Rows.Count OrElse table1.Columns.Count <> table2.Columns.Count Then
Return False
End If
' Row/column count are the same in both tables
' Check each value until difference found or end of table reached
For i As Integer = 0 To table1.Rows.Count - 1
For c As Integer = 0 To table1.Columns.Count - 1
If Not Equals(table1.Rows(i)(c), table2.Rows(i)(c)) Then
Return False
End If
Next
Next
Return True
End Function