SoFunction
Updated on 2025-03-07

Three ways to remove duplicate data in C#

Business Requirements

Recently, I did a batch of data from the source database to the target database. The source database is the original database collected by the acquisition program, so it needs to be processed some (filtered some as empty, too short or too long, illegal characters, duplicate data) and then enter the database.

Among them, you should avoid inserting duplicate data from the target library. This duplicate data may be that the source database itself has duplicate data, and it is inserted to avoid repeated insertion.

Solutions for filtering your own duplicate data

The first: Use the () method

  • method
  • .NET Framework 2.0

It is based on the existingDataView row in, create and return a new oneDataTable

Overload list

name illustrate
() Create and return a new DataTable based on the rows in the existing DataView.

Powered by the .NET Compact Framework.

(String) Create and return a new DataTable based on the rows in the existing DataView.

Powered by the .NET Compact Framework.

(Boolean, String[]) Create and return a new DataTable based on the rows in the existing DataView.

Powered by the .NET Compact Framework.

(String, Boolean, String[]) Create and return a new DataTable based on the rows in the existing DataView.

Powered by the .NET Compact Framework.

Example code

 public static DataTable Distinct(DataTable dt, string[] filedNames)
  {
   DataView dv = ;
   DataTable DistTable = ("Dist", true, filedNames);
   return DistTable;
  }

The second method: loop traversal +()

Use the for loop to traverse the data rows of DataTable, use the method to determine whether it is duplicated. If it is duplicated, use (Index) to delete the duplicated row.

Depend on the code specifically.

Code Example

 public DataTable GetDistinctSelf(DataTable SourceDt, string filedName)
  {
 for (int i =  - 2; i > 0; i--)
   {
    DataRow[] rows = (("{0}='{1}'", filedName, [i][filedName]));
    if ( > 1)
    {
     (i);
    }
   }
   return SourceDt;  
  }

The third method

Use dual loop traversal (not recommended)

 public DataTable GetDistinctSelf(DataTable SourceDt, string filedName)
  {
   for (int i =  - 2; i > 0; i--)
   {
    string title = [0][filedName].ToString();
    for (int j = i + 1; j > 0; i--)
    {
     if ([j][filedName].ToString() == title)
     {
      (i);

     }
    }

   }
   return SourceDt;
   }

This is the end of this article about three methods of removing DataTable duplicate data in C#. For more related content on removing DataTable duplicate data in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!