Business requirements
Recently, a batch of data from the source database has been exported to the target database. The source database is the original database collected by the collection program, so it needs some processing (filtering some empty, too short or too long, illegal characters, repeating data) and then in the storage.
Where you want to avoid inserting duplicate data into the target library. This duplication may be that the source database itself has duplicate data, and there is an insert that avoids repeated insertions.
Filtering your own data deduplication solution first: Using the Dataview.totable () method Dataview.totable method
. NET Framework 2.0Itsbased on existingDataViewcreates and returns a newDataTable. Overloaded List
name |
Description |
Dataview.totable () |
Creates and returns a new DataTable, based on the rows in the existing DataView . Supported by the. NET Compact Framework. |
Dataview.totable (String) |
Creates and returns a new DataTable, based on the rows in the existing DataView . Supported by the. NET Compact Framework. |
Dataview.totable (Boolean, string[]) |
Creates and returns a new DataTable, based on the rows in the existing DataView . Supported by the. NET Compact Framework. |
Dataview.totable (String, Boolean, string[]) |
Creates and returns a new DataTable, based on the rows in the existing DataView . Supported by the. NET Compact Framework. |
Instance Code
Public Static string [] filednames) { = dt. DefaultView; = dv. ToTable ("Dist"true, filednames); return disttable; }
The second method: Looping through +datatable.select ()
Use the For loop to iterate through the data rows of the DataTable, use the DataTable.Select method to determine whether duplicates are repeated, and if so, use DataTable.Rows.RemoveAt (Index) to remove the duplicate line.
See the code specifically.
code example
PublicDataTable Getdistinctself (DataTable Sourcedt,stringfiledname) { for(inti = SourceDt.Rows.Count-2; i >0; i--) {datarow[] rows= Sourcedt.select (string. Format ("{0}= ' {1} '", Filedname, Sourcedt.rows[i][filedname])); if(Rows. Length >1) {SourceDt.Rows.RemoveAt (i); } } returnSourcedt; }
The third method of
Using double loop traversal (not recommended)
PublicDataTable Getdistinctself (DataTable Sourcedt,stringfiledname) { for(inti = SourceDt.Rows.Count-2; i >0; i--) { stringtitle = sourcedt.rows[0][filedname]. ToString (); for(intj = i +1; J >0; i--) { if(Sourcedt.rows[j][filedname]. ToString () = =title) {SourceDt.Rows.RemoveAt (i); } } } returnSourcedt; }
Three ways to remove data from DataTable duplication