Monday 19 March 2012

Remove Whitespace & Duplicate Records depending on primary key & Remove Special Characters


Remove Whitespace

Public Shared Function RemoveWhitespace(ByVal str As String) As String
        Try
            Return New Regex("\s*").Replace(str, String.Empty)
        Catch generatedExceptionName As Exception
            Return str
        End Try
    End Function
==========================================================================
Remove Duplicates

If ds.Tables(0).Rows.Count > 0 Then
                Dim dt As New DataTable()
                dt = RemoveDuplicates(ds.Tables(0))
                ds.Tables.Clear()
                ds.Tables.Add(dt)
End If

Shared Function RemoveDuplicates(ByVal dt As DataTable) As DataTable
   Return ( _
    From row In dt.Rows.OfType(Of DataRow)() _
     Group row By GroupKey = row.Field(Of Int64)("PKProductID") Into g = Group _
     Select g.OrderBy(Function(r) r.Field(Of Int64)("PKProductID")).First()).CopyToDataTable()

End Function
--------------------- ( OR ) -----------------------------------

Public Shared Function RemoveDuplicateStores(ByVal dTable As DataTable, ByVal colName As String) As DataTable

        Dim hTable As New Hashtable()
        Dim duplicateList As New ArrayList()

        'Add list of all the unique item value to hashtable, which stores combination of key, value pair.  
        'And add duplicate item value in arraylist.   

        For Each drow1 As DataRow In dTable.Rows
            If hTable.Contains(drow1(colName)) Then
                duplicateList.Add(drow1)
            Else
                hTable.Add(drow1(colName), String.Empty)
            End If
        Next

        'Removing a list of duplicate items from datatable.  
        For Each drow2 As DataRow In duplicateList
            dTable.Rows.Remove(drow2)
        Next

        'Datatable which contains unique records will be return as output.     
        Return dTable
End Function

=======================================================

''' <summary>
    '''  This funcation escape the some characters
    ''' </summary>
    ''' <param name="tempQuery"></param>
    ''' <returns name="return the query string without parmeters"></returns>
    ''' <remarks></remarks>

Public Shared Function RemoveSpecialCharacters(ByVal tempQuery As String) As String
        tempQuery = tempQuery.ToLower
        Dim strEscape() As String = {"#", """", "&quot;", "\u0022", "&apos;", "\u0027", "&", "\u0026", "<", "&lt;", "\u003c", ">", "&gt;", "\u003e", "'"}
        For Each s As String In strEscape
            tempQuery = tempQuery.Replace(HttpUtility.UrlEncode(s), "")
        Next
        tempQuery = tempQuery.Replace("""", "")
        tempQuery = tempQuery.Replace("%27", "")
        Return tempQuery
End Function


No comments:

Post a Comment