I’m having a simple problem, but I don’t know what is wrong. I’m trying to remove all duplicate records from a DataTable using the following code:
Private Function removeDuplicate(ByVal dTable As DataTable) As DataTable Dim row1 As DataRow Dim row2 As DataRow Const wantedColumn As Int16 = 1 Dim tableSize As Integer = dTable.Rows.Count For i As Integer = 0 To tableSize - 2 row1 = dTable.Rows(i) For j As Integer = i + 1 To tableSize - 1 row2 = dTable.Rows(j) If j >= tableSize Then Exit For End If If row1(wantedColumn) = row2(wantedColumn) Then row2.Delete() tableSize = tableSize - 1 End If Next Next Return dTable End Function
The problem is: even though I’m updating the tableSize every time a row is deleted, it seems that the For loops are iterating until the original size (for original size I mean the dataTable size before any record be deleted). Does anyone have any ideas about what is wrong?