VB.net DT distinct 功能

by Andy
'*----------------------------------------------------------------
'*
'*         函数名称:    DT distinct 功能
'*         摘    要:   用法 dt = SelectDistinct("新DT名", 需要过滤的DT, "字段名")
'*
'*----------------------------------------------------------------

#Region "DT 临时表 去重复 DT 内 distinct -停止使用"

    Public Function SelectDistinct(ByVal TableName As String, _
                                ByVal SourceTable As DataTable, _
                                ByVal FieldName As String) As DataTable
        Dim dt As New DataTable(TableName)
        dt.Columns.Add(FieldName, SourceTable.Columns(FieldName).DataType)
        Dim dr As DataRow, LastValue As Object
        For Each dr In SourceTable.Select("", FieldName)
            If LastValue Is Nothing OrElse Not ColumnEqual(LastValue, dr(FieldName)) Then
                LastValue = dr(FieldName)
                dt.Rows.Add(New Object() {LastValue})
            End If
        Next
        'If Not ds Is Nothing Then ds.Tables.Add(dt)
        Return dt
    End Function

    Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean
        '
        ' Compares two values to determine if they are equal. Also compares DBNULL.Value.
        '
        ' NOTE: If your DataTable contains object fields, you must extend this
        ' function to handle the fields in a meaningful way if you intend to group on them.
        '
        If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both are DBNull.Value.
        If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only one is DBNull.Value.
        Return A = B                                                ' Value type standard comparison
    End Function

#End Region

You may also like

Leave a Comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据