Eliminate Duplicate Rows From DataSet in vb.net

Hello all.

My oldest daughter is suppose to go to court Friday. It might possibly be delayed, but I got to tell you nothing pains a parent more than watching one’s child suffer needlessly. We struggle, we beg, we plead but to no avail. Is it a reflection on myself as a parent? This is the question I struggle with most. I know I could have done some things better. I know there were some things I have done that were right. But I can’t save her anymore and that part just drives me crazy.

Anyway, wrote this little routine to remove duplicate rows from a dataset that has no primary key. How did I end up with a dataset without a primary key? Well that I can’t answer because I didn’t have any control over it.

Make it a great day! And I will try, really I will try, to do that.


Join me on Facebook

Sub EliminateDuplicates(ByVal dt As DataTable)
        Dim ssort As String = ""
        Dim col As DataColumn
        Dim scol As DataColumn
        For Each scol In dt.Columns
            If ssort <> "" Then ssort &= ","
            ssort &= scol.ColumnName
        Next

        dt.DefaultView.Sort = ssort
        Dim i As Integer, bEquals As Boolean, c As Integer, ccount As Integer
        ccount = dt.Columns.Count
        For i = dt.Rows.Count – 1 To 1 Step -1
            bEquals = True
            For c = 0 To ccount – 1
                If dt.DefaultView(i)(c).ToString() <> dt.DefaultView(i – 1)(c).ToString() Then
                    bEquals = False
                    Exit For
                End If
            Next c

            If bEquals Then
                dt.DefaultView(i).Delete()
            End If
        Next

    End Sub

Technorati Tags: ,,,,,,,,,,,

Windows Live Tags: vb.net,.NET Framework,csharp,rows,Eliminate,Duplicate,DataSet,DataTable,DataColumn,DefaultView,Sort,Delete

 
Advertisements
  1. #1 by Frank on August 7, 2008 - 8:53 pm

    Kids are gonna do what they\’re gonna do.  There are no guarantees.  The saintly child can turn out to be a Serial killer, and the hellion turns out to be a Cardinal.
     
    You never know.
     
    All you can do it try your best to instill whatever values you can, then you need to wind em up and watch them make their own choices.  Just need to be there for them, good or bad, and be honest with them.
     
    IMO.

  2. #2 by Tony on August 22, 2009 - 3:29 am

    \’Dim col As DataColumn\’ is unused.

  3. #3 by Tony on August 22, 2009 - 3:37 am

    Just tried your code after struggling with iterations to check for duplicates and I can\’t believe it works. I have yet to analyse your code to see what it is doing but great work. Thanks a lot. You\’ve got yourself a credit, if that\’s ok?

  1. [RESOLVED]Remove duplicate records from a DataSet | ASP Questions & Answers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: