0

I am sorting a datable 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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

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 dup-group. If you want to order the group use grp.OrderBy.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I am working in an old framework. I can possibly tell them to update the framework. Is any non linq solution a bunch of code? Cool linq solution by the way! –  Jun 23 '15 at 15:41
  • How do you convert a datable to Enumerable in VB.Net? –  Jun 23 '15 at 15:48
  • @npl77: I have shown it above, with `AsEnumerable` ;) – Tim Schmelter Jun 23 '15 at 15:52
  • I know I am using vb.net so have to convert the code, but vb.net has no "AsEnumerable" ext method –  Jun 23 '15 at 15:53
  • @npl77: its in the `System.Data.DataSetExtensions` dll. You need to add a reference to it. http://stackoverflow.com/questions/9217272/datatable-does-not-contain-definition-for-asenumerable However, that's from .NET 3.5. – Tim Schmelter Jun 23 '15 at 15:57