Three ways to remove data from DataTable duplication

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.