Hi iammann,
Below is the right way of finding the duplicate records and save them into new datatable.
C#
DataTable dt = new DataTable();
dt.Columns.Add("RowId");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Quality");
dt.Rows.Add(1, "Tom", "Hilton", "ABC");
dt.Rows.Add(1, "Tom", "Hilton", "ABC");
dt.Rows.Add(3, "Test3", "Sample3", "AWR");
dt.Rows.Add(4, "Test4", "Sample4", "XYZ");
dt.Rows.Add(5, "Test5", "Sample5", "BCA");
DataTable resultTable = new DataTable();
resultTable.Columns.Add(new DataColumn("RowId"));
resultTable.Columns.Add(new DataColumn("FirstName"));
resultTable.Columns.Add(new DataColumn("LastName"));
resultTable.Columns.Add(new DataColumn("Quality"));
var result = from c in dt.AsEnumerable()
group c by new
{
RowId = c.Field<string>("RowId"), //column names for checking duplicate values.
FirstName = c.Field<string>("FirstName"),
LastName = c.Field<string>("LastName"),
Quality = c.Field<string>("Quality")
} into g
where g.Count() > 1
select new
{
g.Key.RowId,
g.Key.FirstName,
g.Key.LastName,
g.Key.Quality
};
for (int i = 0; i < result.ToList().Count; i++)
{
var item = result.ToList()[i];
resultTable.Rows.Add(item.RowId, item.FirstName, item.LastName, item.Quality);
}