我想用LINQ來比較2個DataTables中的列的差異。兩列的類型相同(Int32),兩者的查詢是相同的,但只有一列給出了預期的結果。另一個並不總是趕上變化。它將從1到0的值的更改註冊,但忽略從0到1的值更改。下面顯示了兩條LINQ語句。不一致的LINQ結果比較數據表
這工作:
Dim result = From table1 In newTable
Where Not (From table2 In origTable
Where (table2("Ack Required") = table1("Ack Required"))).Any()
Select table1.ItemArray()
這工作時,值更改爲1,但什麼都不做,當它變爲0:
Dim result2 = From table1 In newTable
Where Not (From table2 In origTable
Where (table2("Active") = table1("Active"))).Any()
Select table1.ItemArray()
什麼我錯在這裏做什麼?
編輯:這是一個循環的實現,我發現,它是我想要做的事:
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
你似乎並沒有被這兩個表上加入行任何類型的鍵比較匹配的行,所以我不確定任何一個正在做你的想法。表格中是否有某種ID字段? – Mark
這很有道理。沒有ID字段。我將不得不看看數據,但我認爲兩個字段的組合會產生一個獨特的價值。 – thephez