score:2

Accepted answer
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();

        }
    }
}

score:2

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;
}

Related Query

More Query from same tag