简体   繁体   中英

.net: DataTable setting all non numeric values of data table as NULL

Need help to format datatable in .net, giving code below in vb.net

Dim dt As New DataTable
            dt.Columns.Add("a", GetType(System.String))
            dt.Columns.Add("b", GetType(System.String))

            dt.Rows.Add("N/A", "22")
            dt.Rows.Add("99", "22.3")
            dt.Rows.Add("Not Available", "#Error")
            dt.Rows.Add("5.5", "10")

' Need Suggestions to transform data table in .net c#/vb

'Convert all Non Numeric Values of Data Table to NULL, trying to avoid Looping and updating each row by using regex

            'Result dt
            'a      b       
            'null   22
            '99     22.3
            'null   null
            '5.5    10

Hm if u don't need the table to contain the non-numeric value to begin with and don't get it handed like this, u can write your own method to add rows, which checks before adding them if they are numeric or not.

Otherwise i can't think of anything preventing a loop, if you don't want to use regex for some reasons you can check if a convertion is possible (Convert.ToDouble)

Posting working code (vb)

 Dim dt As New DataTable
            dt.Columns.Add("a")
            dt.Columns.Add("b")
            dt.Columns.Add("c")

Dim typeList As New List(Of Type)
            typeList.Add(GetType(Integer))
            typeList.Add(Nothing)
            typeList.Add(GetType(Decimal))

            dt.Rows.AddRow(typeList, {10, "23ff", "Test if Numeric 45.6"})
            dt.Rows.AddRow(typeList, {"Ten", "We are here", "89"})

The exension methood is

<System.Runtime.CompilerServices.Extension()> _
    Public Sub AddRow(dr As DataRowCollection, ByRef objTypeConversion As List(Of Type), objValue As Object())
        Dim length = objTypeConversion.Count

        If length <> objValue.Length Then
            Throw New Exception("Data Types must be provided for all Values")
        End If

        Dim _n As Integer = 0
        objTypeConversion.ForEach(Function(t)
                                      If t IsNot Nothing Then
                                          Try
                                              Dim converter = TypeDescriptor.GetConverter(t)
                                              If converter IsNot Nothing Then
                                                  objValue(_n) = CTypeDynamic(converter.ConvertFrom(objValue(_n)), t)
                                              End If
                                          Catch ex As Exception
                                              objValue(_n) = DBNull.Value
                                          End Try
                                      End If
                                      _n = _n + 1
                                  End Function)

        'Logic to convert into provided datatype
        dr.Add(objValue)
    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM