Search code examples
vb.netdatatableado.net

How to check if a value is duplicate and if duplicate need to delete both line in Datatable


I have a Datatable with few Line of Data, my aim is to check whether there is any Duplicate value and if there a duplicate line, i that Duplicate Value & i need to remove both lines that created duplicate.

Input

InPut

my expected Output would be

OutPut

Any Suggestion will be helpful

Thanks in Advance

Thanks & Regards Harsha


Solution

  • You need some way of getting a unique value for each row which depends only on the data in the row, this is usually known as a hash value.

    Then you can use whatever method you like to bin the data and choose only the bins with one item.

    For example, with just a DataGridView and a Button on a Form:

    Public Class Form1
    
        Dim dt As New DataTable
    
        Function RowHash(dr As DataRow) As Long
            'TODO: Make a hash function which will not overflow a Long.
            Dim h As Long = 0
            For Each itm In dr.ItemArray
                h = (31 * h) + itm.GetHashCode()
            Next
    
            Return h
    
        End Function
    
        Sub MakeTestData()
            dt.Columns.Add("Name")
            dt.Columns.Add("P")
            For i = 0 To 9
                Dim nr = dt.NewRow()
                nr("Name") = Chr(65 + (i Mod 6))
                nr("P") = i Mod 6
                dt.Rows.Add(nr)
            Next
    
            DataGridView1.DataSource = dt
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim newDataTable = dt.Clone()
            Dim uniqueData = dt.AsEnumerable().
                GroupBy(Function(r) RowHash(r)).
                Select(Function(s) New With {.n = s.Count, .data = s.First().ItemArray}).
                Where(Function(t) t.n = 1)
    
            For Each u In uniqueData
                Dim nr = newDataTable.NewRow()
                nr.ItemArray = u.data
                newDataTable.Rows.Add(nr)
            Next
    
            DataGridView1.DataSource = newDataTable
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MakeTestData()
    
        End Sub
    
    End Class
    

    Before and after clicking the button:

    enter image description here