I have checked the whole site and googled on the net but was unable to find a simple solution to this problem.
I have a datatable which has about 20 columns and 10K
I think this must be the best way to remove duplicates from Datatable by using Linq and moreLinq
Code:
Linq
RemoveDuplicatesRecords(yourDataTable);
private DataTable RemoveDuplicatesRecords(DataTable dt)
{
var UniqueRows = dt.AsEnumerable().Distinct(DataRowComparer.Default);
DataTable dt2 = UniqueRows.CopyToDataTable();
return dt2;
}
Blog Article : Remove Duplicate rows records from DataTable Asp.net c#
// Distinctby column name ID
var valueDistinctByIdColumn = yourTable.AsEnumerable().DistinctBy(row => new { Id = row["Id"] });
DataTable dtDistinctByIdColumn = valueDistinctByIdColumn.CopyToDataTable();
Note: moreLinq need to add library.
In morelinq you can use function called DistinctBy in which you can specify the property on which you want to find Distinct objects.
Blog article : Using moreLinq DistinctBy method to remove duplicate records