Search code examples
vb.net

Filtering Last Duplicate Occurrence In A Datatable


I am sorting a datatable with the following code:

Dim sortedExtRecords1 As DataTable
sortedExtRecords1 = parsedDataset.Tables("Detail").Clone

Dim dvParsedDataset1 As New DataView(parsedDataset.Tables("Detail"))
dvParsedDataset1.Sort = AuthorizatonConstants.Authorization_ID

sortedExtRecords1 = dvParsedDataset1.ToTable("Detail")

I can further filter the results to only return non duplicates and if there is duplicates I want the last record of the duplicate only. My duplicates vary in size some ids can have 3 duplicates others 5 others 10, I am not sure if this matters


Solution

  • You can use LINQ:

    DataTable nonDups = parsedDataset.Tables("Detail").AsEnumerable()
        .GroupBy(row => row.Field<string>("Authorization_ID"))
        .OrderBy(grp => grp.Key)
        .Select(grp => grp.Last())
        .CopyToDataTable();
    

    This selects the last row of each duplicate group. If you want to order the group, use grp.OrderBy.