Remove duplicates rows in a datatable

The sample code below demonstrates how to remove duplicate rows in a datatable.

DataTable dt = new DataTable();
dt.Columns.Add(“id”, typeof(int));
dt.Columns.Add(“name”, typeof(string));

dt.Rows.Add(1, “test 1”);
dt.Rows.Add(2, “test 2”);
dt.Rows.Add(3, “test 3”);
dt.Rows.Add(4, “test 4”);
dt.Rows.Add(4, “test 4”);
dt.Rows.Add(5, “test 5”);
dt.Rows.Add(5, “test 5”);

DataView view = new DataView(dt);
DataTable distinctValues = view.ToTable(true, “id”, “name”);

Hope this helps.

Thanks – JOxin

Leave a comment