Pergunta

Using VB.NET I am trying to remove duplicate datatable records based on several of the fields. Im using distinct and can only seem to be able to retrieve the fields in my new datatabe. Here is an example...

Public Shared Function GetDistinctRecords(dt As DataTable, Columns As String()) As DataTable
    Dim dtUniqRecords As New DataTable()
    dtUniqRecords = dt.DefaultView.ToTable(True, Columns)
    Return dtUniqRecords
End Function

here is how I am calling the method.

Dim TobeDistinct As String() = {"Date", "StartTime", "RoomID", "Room"}
Dim dt As DataTable = GetDistinctRecords(initialDt, TobeDistinct)

I need to retrieve the complete record which has about 30 columns. The only distinct columns are what I am specifying in my TobeDistinct string array.

Foi útil?

Solução

You can make a complexed query on the existing datatable.

Private Function DistinctList(dTable As DataTable, dt As Date, time As String, _
                              RoomId As Integer, room As String) As List(Of DataRow)
 Dim query = (From dr As DataRow In dTable.Rows Where
              Date.Parse(dr("date").ToString) = dt And
              dr("StartTime").ToString = time And
              Convert.ToInt32(dr("RoomId").ToString) = RoomId And
              dr("Room").ToString = room).ToList

 Return query
End Function
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top