0

How do I delete the duplicate rows from the datatable where there are combinations of same name and dept combinations? I need to keep one entry.

DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Name");
dt.Columns.Add("Dept");

dt.Rows.Add(1, "Test1", "Sample1");
dt.Rows.Add(2, "Test2", "Sample2");
dt.Rows.Add(3, "Test3", "Sample3");
dt.Rows.Add(4, "Test4", "Sample4");  // Duplicate 
dt.Rows.Add(5, "Test4", "Sample4");  // Duplicate 
dt.Rows.Add(6, "Test4", "Sample4");  // Duplicate 
dt.Rows.Add(7, "Test4", "Sample5");  

Result data table should be,

dt.Rows.Add(1, "Test1", "Sample1");
dt.Rows.Add(2, "Test2", "Sample2");
dt.Rows.Add(3, "Test3", "Sample3");
dt.Rows.Add(4, "Test4", "Sample4");  
dt.Rows.Add(6, "Test4", "Sample5");  

How can I do this in c#

2
  • A "silly" algorithm would be to loop over each row, create a copy, store it in a list if not stored yet. Before storing you do your comparison. Your list will have no duplicates. The bigger the table, the slower this algorithm will be. Commented Apr 7, 2020 at 22:37
  • 1
    I think the answer to this question may be what you need: stackoverflow.com/questions/3242892/… Commented Apr 7, 2020 at 23:12

2 Answers 2

2

Simple

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Dept", typeof(string));

            dt.Rows.Add(1, "Test1", "Sample1");
            dt.Rows.Add(2, "Test2", "Sample2");
            dt.Rows.Add(3, "Test3", "Sample3");
            dt.Rows.Add(4, "Test4", "Sample4");  // Duplicate 
            dt.Rows.Add(5, "Test4", "Sample4");  // Duplicate 
            dt.Rows.Add(6, "Test4", "Sample4");  // Duplicate 
            dt.Rows.Add(7, "Test4", "Sample5");

            DataTable dt2 = dt.AsEnumerable()
                .OrderBy(x => x.Field<int>("id"))
                .GroupBy(x => new { name = x.Field<string>("Name"), dept = x.Field<string>("Dept") })
                .Select(x => x.First())
                .CopyToDataTable();

        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm using .net core where I don't have AsEnumerable() extension
The following article from Oct 2018 (before core 3.0) says core has linq. Did you try to put a "using System.Linq" at top of module? learn.microsoft.com/en-us/dotnet/csharp/tutorials/…
Adding System.Data.DataSetExtensions nuget package helped
Excellent solution, thanks!
2

Here is a function that I got from someone, somewhere:

Usage:

List<string> columnName = new List<string> { "ID", "coulmn1", "coulmn_2", "Another", "however_many_columns_you_want_really" };
dataGrid = RemoveDuplicatesFromDataTable(dataGrid, columnName);

Function:

static DataTable RemoveDuplicatesFromDataTable(DataTable table, List<string> keyColumns)
{
    Dictionary<string, string> uniquenessDict = new Dictionary<string, string>(table.Rows.Count);
    StringBuilder stringBuilder = null;
    int rowIndex = 0;
    DataRow row;
    DataRowCollection rows = table.Rows;
    while (rowIndex < rows.Count - 1)
    {
        row = rows[rowIndex];
        stringBuilder = new StringBuilder();
        foreach (string colname in keyColumns)
        {
            //stringBuilder.Append(((double)row[colname]));
            stringBuilder.Append(row[colname]);
        }
        if (uniquenessDict.ContainsKey(stringBuilder.ToString()))
        {
            rows.Remove(row);
        }
        else
        {
            uniquenessDict.Add(stringBuilder.ToString(), string.Empty);
            rowIndex++;
        }
    }

    return table;
}

1 Comment

Very good solution, I just add my two cents. while (rowIndex < rows.Count - 1) will return 2 lines with same values in the DT , while if you want to have only one (unique) row, correct code is while (rowIndex < rows.Count )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.