Reputation: 1
I have a datatable with say 3 columns like below and it contains duplicate records. I want find the duplicate FileName records with same Id field and delete the older one by comparing the date field column. How can I do it in Linq?
Current
Test.pdf || 06/22/2020 || 1
Test1.pdf || 06/22/2020 || 2
Test.pdf || 06/21/2020 || 1
Test.pdf || 06/21/2020 || 3
Delete this record - Test.pdf with Id = 1 because it was updated on 06/21.
Expected
Test.pdf ||06/22/2020 || 1
Test1.pdf ||06/22/2020 || 2
Test.pdf ||06/21/2020 || 3
I'm able to fetch the duplicate records using below snippet by grouping the record by fileName but I'm not sure how to group the records by Id also
var filteredList = DT.AsEnumerable()
.GroupBy(dr => dr.Field<string>("FileName"))
.Select(g => g.OrderByDescending(dr => dr.Field<DateTime>("CreationDate")).First())
.ToList();
Upvotes: 0
Views: 177
Reputation: 56
You can change what you're doing with the snippet that selects duplicates to get the filtered list of items you're looking for.
First, group the DataRows by the column "FileName". Now that you have the groups, order each group by the column CreatedDate in descending order and take the first DataRow from each group.
var filteredList = DT.AsEnumerable()
.GroupBy(dr => dr.Field<string>("FileName"))
.Select(g => g.OrderByDescending(dr => dr.Field<DateTime>("CreatedDate")).First())
.ToList();
Upvotes: 1