Eliminare le righe duplicate da un DataTable

by enrico 29. September 2008 01:07

Ciao a tutti. Come riuscire a cancellare da un datatable tutte le righe che sono uguali? Questa semplice funzione consente proprio di passare il datatable e restituirlo con le righe doppie eliminate.

   1:  Function EliminateDuplicates(ByVal dt As DataTable) As DataTable
   2:          Dim ssort As String = ""
   3:          Dim col As DataColumn
   4:          Dim scol As DataColumn
   5:          For Each scol In dt.Columns
   6:              If ssort <> "" Then ssort &= ","
   7:              ssort &= scol.ColumnName
   8:          Next 
   9:   
  10:          dt.DefaultView.Sort = ssort
  11:          Dim i As Integer, bEquals As Boolean, c As Integer, ccount As Integer
  12:          ccount = dt.Columns.Count
  13:          For i = dt.Rows.Count - 1 To 1 Step -1
  14:              bEquals = True
  15:              For c = 0 To ccount - 1
  16:                  If dt.DefaultView(i)(c).ToString() <> dt.DefaultView(i - 1)(c).ToString() Then
  17:                      bEquals = False
  18:                      Exit For
  19:                  End If
  20:              Next c 
  21:   
  22:              If bEquals Then
  23:                  dt.DefaultView(i).Delete()
  24:              End If
  25:          Next 
  26:   
  27:         Return dt
  28:      End Sub 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

vb.net | Tricks & Tips

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by Esia Software 1.4.0.0

Informazioni sull'autore

Sono un cittadini di Trieste, ho 33 anni e faccio lo sviluppatore nella mia azienda che si chiama Esia Software.

Commenti recenti

Comment RSS

Post recenti

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Info legali

Le opinioni espresse in questo blog sono strettamente personali e ogni persona è responsabile dei commenti che inserisce.

RecentComments

Comment RSS