无法根据字段值从数据表中获取不同的记录

时间:2014-04-10 18:21:08

标签: vb.net datatable vb.net-2010

使用VB.NET我试图根据几个字段删除重复的数据表记录。我使用distinct,似乎只能检索我的新数据中的字段。这是一个例子......

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

这是我如何调用该方法。

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

我需要检索包含大约30列的完整记录。唯一不同的列是我在TobeDistinct字符串数组中指定的。

1 个答案:

答案 0 :(得分:0)

您可以对现有数据表进行复杂查询。

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