Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:49:33+00:00 2026-06-12T22:49:33+00:00

Which way is the best for removing duplicates from a dataTable for multiple columns?I

  • 0

Which way is the best for removing duplicates from a dataTable for multiple columns?I mean below code is only for a single column.

public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
   Hashtable hTable = new Hashtable();
   ArrayList duplicateList = new ArrayList();

   //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
   //And add duplicate item value in arraylist.
   foreach (DataRow drow in dTable.Rows)
   {
      if (hTable.Contains(drow[colName]))
         duplicateList.Add(drow);
      else
         hTable.Add(drow[colName], string.Empty); 
   }

   //Removing a list of duplicate items from datatable.
   foreach (DataRow dRow in duplicateList)
      dTable.Rows.Remove(dRow);

   //Datatable which contains unique records will be return as output.
      return dTable;
}

I tried using string[] colName. It throws error at dTable.Rows.Remove(dRow);

Please suggest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:49:34+00:00Added an answer on June 12, 2026 at 10:49 pm

    The easiest and most readable is using Linq-to-DataTable:

    var groups = from r in dTable.AsEnumerable()
                 group r by new
                 {
                     Col1 = r.Field<String>("Column1"),
                     Col2 = r.Field<String>("Column2"),
                 };
    
    // if you only want the first row of each group:
    DataTable distinctTable = groups.Select(g => g.First()).CopyToDataTable();
    

    Notes: Enumerable.GroupBy groups the DataRows by an anonymous type with two properties(Col1 and Col2) which are initialized from a DataRow fields Column1 and Column2.

    So you get groups of IEnumerable<DataRow>. Enumerable.First() returns the first DataRow of each group (you could also use different methods to select the row you want to keep, for example by ordering by a date field).

    Then CopyToDataTable creates a new DataTable from the (now) distinct DataRows.


    Here’s a possible implementation if you’re using .NET 2:

    implementation of a custom IEqualityComparer<Object[]> for the dictionary:

    class ObjectArrayComparer : IEqualityComparer<Object[]>
    {
        public bool Equals(Object[] x, Object[] y)
        {
            if (x == null && y == null) return true;
            if (x == null || y == null) return false;
            if (x.Length  !=  y.Length) return false;       
    
            for (int i = 0; i < x.Length; i++)
            {
                if (x[i] == null && y[i] == null) continue;
                if (x[i] == null || y[i] == null) return false;
                if (!x[i].Equals(y[i])) return false;
            }
            return true;
        }
    
        public int GetHashCode(Object[] obj)
        {
            int hash = 0;
            if (obj != null)
            {
                hash = (hash * 17) + obj.Length;
                foreach (Object o in obj)
                {
                    hash *= 17;
                    if (o != null) hash = hash + o.GetHashCode();
                }
            }
            return hash;
        }
    }
    

    your RemoveDuplicateRows method:

    public DataTable RemoveDuplicateRows(DataTable dTable, String[] colNames)
    {
        var hTable = new Dictionary<object[], DataRow>(new ObjectArrayComparer());
    
        foreach (DataRow drow in dTable.Rows)
        {
            Object[] objects = new Object[colNames.Length];
            for (int c = 0; c < colNames.Length; c++)
                objects[c] = drow[colNames[c]];
            if (!hTable.ContainsKey(objects))
                hTable.Add(objects, drow);
        }
    
        // create a clone with the same columns and import all distinct rows
        DataTable clone = dTable.Clone();
        foreach (var kv in hTable)
            clone.ImportRow(kv.Value);
    
        return clone;
    }
    

    testing:

    var table = new DataTable();
    table.Columns.Add("Colum1", typeof(string));
    table.Columns.Add("Colum2", typeof(int));
    table.Columns.Add("Colum3", typeof(string));
    
    Random r = new Random();
    for (int i = 0; i < 100; i++)
    {
        table.Rows.Add("Colum1_" + r.Next(1, 10), r.Next(1, 10), "Colum3_" + r.Next(1, 10));
    }
    int rowCount = table.Rows.Count; // 100
    var unique = RemoveDuplicateRows(table, new[] { "Colum1", "Colum2" });
    int uniqueRowCount = unique.Rows.Count; // around 55-65
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which is the best way (in performance and security) to send multiple parameters to
Which method provides the best performance when removing the time portion from a datetime
It should work similar to the android home screen. Which way has the best
Which is the best way of implementing a language translation for mutli -lang site.
Which is the best way to sanitize content? An example... Example - Before sanitize:
Which is the best way to read and parse an Fixed Width Text File
Which is the best way to manage a website with one or more mirrors
Which is the best way to registered event for example if I wanto to
Which is the best way to loop thru an list? is for loop better
Which is the best way to perform the following: Lets say i have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.