Select distinct record from datatable, vb.net

Recently i shared my ideas on How to merge and sort two datatables. Completition of this task raise an another issue to Select distinct record from the merged datatable. I found many lengthy process but these are not ok. After some goggling i found a very easy process. Just create a dataview object of the datatable and

dt = dt.DefaultView.ToTable(true, param)

Where param will hold the Column Name for which you want to make distinct values.
So there is no need for any change in How to merge and sort two datatables process, we will just update the code on return statement of the function.

Private Function MergeAndSortTables(ByVal Table1 As DataTable, ByVal Table2 As DataTable, ByVal SortBy As String) As DataTable
  Table1.Merge(Table2)
  Dim View As DataView = Table1.DefaultView
  View.Sort = SortBy

  Return View.ToTable(true)
End Function

It’s funny….

Leave a Reply

Your email address will not be published. Required fields are marked *