1

Consider my data table

ID Name
1  AAA
2  BBB
3  CCC
1  AAA
4  DDD

Final Output is

2 BBB
3 CCC
4 DDD

How can i remove the rows in the data table using Vb.Net Any help is appreciated.

2
  • What is the source of this data?
    – Kinexus
    Commented Oct 7, 2011 at 11:38
  • 2
    What is a duplicate, a row with same Name or a row with same name and ID? Commented Oct 7, 2011 at 13:08

4 Answers 4

7

Following works if you only want the distinct rows(skip those with same ID and Name):

Dim distinctRows = From r In tbl
       Group By Distinct = New With {Key .ID = CInt(r("ID")), Key .Name = CStr(r("Name"))} Into Group
       Where Group.Count = 1
       Select Distinct
' Create a new DataTable containing only the unique rows '
Dim tblDistinct = (From r In tbl
       Join distinctRow In tblDistinct
       On distinctRow.ID Equals CInt(r("ID")) _
       And distinctRow.Name Equals CStr(r("Name"))
       Select r).CopyToDataTable

If you want to remove the dups from the original table:

Dim tblDups = From r In tbl
       Group By Dups = New With {Key .ID = CInt(r("ID")), Key .Name = CStr(r("Name"))} Into Group
       Where Group.Count > 1
       Select Dups
Dim dupRowList = (From r In tbl
       Join dupRow In tblDups
       On dupRow.ID Equals CInt(r("ID")) _
       And dupRow.Name Equals CStr(r("Name"))
       Select r).ToList()

For Each dup In dupRowList 
    tbl.Rows.Remove(dup)
Next

Here is your sample-data:

Dim tbl As New DataTable
tbl.Columns.Add(New DataColumn("ID", GetType(Int32)))
tbl.Columns.Add(New DataColumn("Name", GetType(String)))
Dim row = tbl.NewRow
row("ID") = 1
row("Name") = "AAA"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 2
row("Name") = "BBB"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 3
row("Name") = "CCC"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 1
row("Name") = "AAA"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 4
row("Name") = "DDD"
tbl.Rows.Add(row)
2
  • this removed the duplicates, but it does not keep one occurence. So if i had 2 same rows, both rows were removed.
    – Sophie
    Commented Jul 12, 2023 at 19:56
  • @Sophie: because that was desired in this question. If you want to keep one dont use the Where-filter but use Select Group.First() Commented Jul 12, 2023 at 22:29
7

You can use the DefaultView.ToTable method of a DataTable to do the filtering like this:

 Public Sub RemoveDuplicateRows(ByRef rDataTable As DataTable)
    Dim pNewDataTable As DataTable
    Dim pCurrentRowCopy As DataRow
    Dim pColumnList As New List(Of String)
    Dim pColumn As DataColumn

    'Build column list
    For Each pColumn In rDataTable.Columns
        pColumnList.Add(pColumn.ColumnName)
    Next

    'Filter by all columns
    pNewDataTable = rDataTable.DefaultView.ToTable(True, pColumnList.ToArray)

    rDataTable = rDataTable.Clone

    'Import rows into original table structure
    For Each pCurrentRowCopy In pNewDataTable.Rows
        rDataTable.ImportRow(pCurrentRowCopy)
    Next
End Sub
3
  • you are a hero!!
    – John
    Commented Jun 6, 2019 at 15:23
  • Great answer, the only part of your implementation which I missed was the cloning part. If this was my question I'd mark this as answer, have a nice day! (: Commented Sep 20, 2019 at 12:00
  • Glad this is useful. The Clone method basically copies the structure without the rows, so in this case, a new empty DataTable is assigned to the variable, and then rows are imported into it.
    – David Ruiz
    Commented Oct 15, 2019 at 16:17
0

Assuming you want to check all the columns, this should remove the duplicates from the DataTable (DT):

        DT = DT.DefaultView.ToTable(True, Array.ConvertAll((From v In DT.Columns Select v.ColumnName).ToArray(), Function(x) x.ToString()))

Unless I overlooked it, this doesn't seem to be in the documentation (DataView.ToTable Method), but this also appears to do the same thing:

DT = DT.DefaultView.ToTable(True)
0
''' <summary>
''' Removes duplicate rows from a DataTable based on a specified column.
''' </summary>
''' <param name="TableToModify">The DataTable to modify.</param>
''' <param name="ColumnNameToCompare">The column name to compare for duplicates.</param>
Sub RemoveDuplicatesFromADataTable(ByRef TableToModify As DataTable, ByVal ColumnNameToCompare As String)
    'Start at the top row (Very important and the key for this to work!!).
    Dim Index1 As Integer = 0
    Dim rows As DataRowCollection = TableToModify.Rows
    '
    Do Until Index1 = rows.Count
        'Get the value from the current row.
        Dim primaryRow As DataRow = rows(Index1)
        Dim key As Object = primaryRow(ColumnNameToCompare)

        'Compare with all rows below, starting at the bottom.
        For Index2 As Integer = rows.Count - 1 To Index1 + 1 Step -1
            Dim secondaryRow As DataRow = rows(Index2)
            '
            If key.Equals(secondaryRow(ColumnNameToCompare)) Then
                rows.Remove(secondaryRow)
            End If
        Next
        '
        Index1 += 1
    Loop
End Sub

Notice that TableToModify is a reference.

All credits to: https://www.vbforums.com/showthread.php?870631-RESOLVED-Data-Table-Delete-Duplicates
@jmcilhinney

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.