How to remove duplicate rows from a DataTable....

Someone at work asked this question so I spent five minutes and banged out this code. If it helps one person, it probably will help many so here it is....

--------- Demo Removing Duplicate rows in a DataTable ----------------

using System;

using System.Data;

using System.Collections;

namespace Demo_DataTableRemoveDupRows

{

      class Class1

      {

            [STAThread]

            static void Main(string[] args)

            {

                  // create an example datatable with duplicate rows

                  DataTable tbl = new DataTable();

                  tbl.Columns.Add("ColumnA");

                  tbl.Columns.Add("ColumnB");

                  tbl.Columns.Add("ColumnC");

                  for(int i = 0; i<10; i++)

                  {

                        DataRow nr = tbl.NewRow();

                        nr["ColumnA"] = "A" + i.ToString();

                        nr["ColumnB"] = "B" + i.ToString();

                        nr["ColumnC"] = "C" + i.ToString();

                        tbl.Rows.Add(nr);

                        // duplicate

                        nr = tbl.NewRow();

                        nr["ColumnA"] = "A" + i.ToString();

                        nr["ColumnB"] = "B" + i.ToString();

                        nr["ColumnC"] = "C" + i.ToString();

                        tbl.Rows.Add(nr);

                  }

                  PrintRows(tbl); // show table with duplicates

                  //Create an array of DataColumns to compare

                  //If these columns all match we consider the

                  //rows duplicate.

                  DataColumn[] keyColumns =

                              new DataColumn[]{tbl.Columns["ColumnA"],

                                               tbl.Columns["ColumnA"]};

                  //remove the duplicates

                  RemoveDuplicates(tbl, keyColumns);

                  PrintRows(tbl); // show table again

                  Console.ReadLine(); // pause to view output

            }

            ///

            /// Removes duplicate rows from given DataTable

            ///

            /// Table to scan for duplicate rows

            /// An array of DataColumns

            ///   containing the columns to match for duplicates

            private static void RemoveDuplicates(DataTable tbl,

                                                 DataColumn[] keyColumns)

            {

                  int rowNdx = 0;

                  while(rowNdx < tbl.Rows.Count-1)

                  {

                        DataRow[] dups = FindDups(tbl, rowNdx, keyColumns);

                        if(dups.Length>0)

                        {

                              foreach(DataRow dup in dups)

                              {

                                    tbl.Rows.Remove(dup);

                              }

                        }

                        else

                        {

                              rowNdx++;

                        }

                  }

            }

            private static DataRow[] FindDups(DataTable tbl,

                                              int sourceNdx,

                                              DataColumn[] keyColumns)

            {

                  ArrayList retVal = new ArrayList();

                  DataRow sourceRow = tbl.Rows[sourceNdx];

                  for(int i=sourceNdx + 1; i<tbl.Rows.Count; i++)

                  {

                        DataRow targetRow = tbl.Rows[i];

                        if(IsDup(sourceRow, targetRow, keyColumns))

                        {

                              retVal.Add(targetRow);

                        }

                  }

                  return (DataRow[]) retVal.ToArray(typeof(DataRow));

            }

            private static bool IsDup(DataRow sourceRow,

                                      DataRow targetRow,

                                      DataColumn[] keyColumns)

            {

                  bool retVal = true;

                  foreach(DataColumn column in keyColumns)

                  {

                        retVal = retVal && sourceRow[column].Equals(targetRow[column]);

                        if(!retVal) break;

                  }

                  return retVal;

            }

            private static void PrintRows(DataTable tbl)

            {

                  for(int i=0; i<tbl.Rows.Count; i++)

                  {

                        Console.WriteLine("row: {0}, ColumnA: {1}, ColumnB: {2}", i, tbl.Rows[i]["ColumnA"], tbl.Rows[i]["ColumnB"]);

                  }

            }

      }

}

--------- End Demo Code ----------------

Hope it helps someone....

-Andy

This article is part of the GWB Archives. Original Author: Andy Johns

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.