我想用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