none
Llenar un DataTable con Linq sql

Answers

  • hola

    prueba algo como esto

     

        Public Function LINQToDataTable(Of T)(ByVal varlist As IEnumerable(Of T)) As DataTable
            Dim dtReturn As New DataTable()

            ' column names
            Dim oProps As PropertyInfo() = Nothing

            If varlist Is Nothing Then
                Return dtReturn
            End If

            For Each rec As T In varlist
                ' Use reflection to get property names, to create table, Only first time, others will follow
                If oProps Is Nothing Then
                    oProps = DirectCast(rec.GetType(), Type).GetProperties()
                    For Each pi As PropertyInfo In oProps
                        Dim colType As Type = pi.PropertyType

                        If (colType.IsGenericType) AndAlso (colType.GetGenericTypeDefinition() Is GetType(Nullable(Of ))) Then
                            colType = colType.GetGenericArguments()(0)
                        End If

                        dtReturn.Columns.Add(New DataColumn(pi.Name, colType))
                    Next
                End If

                Dim dr As DataRow = dtReturn.NewRow()

                For Each pi As PropertyInfo In oProps
                    dr(pi.Name) = If(pi.GetValue(rec, Nothing) Is Nothing, DBNull.Value, pi.GetValue(rec, Nothing))
                Next

                dtReturn.Rows.Add(dr)
            Next
            Return dtReturn
        End Function

     

    lo converti a vb.net de este link

    Convert a LINQ Query Resultset to a DataTable

    saludos


    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina
    Wednesday, June 30, 2010 7:37 PM

All replies

  • hola

    te refieres a linq con objetos o a linq to sql o a linq to entities ?

    o sea que coleccion de objetos quieres cargar en el datatable ?

    saludos


    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina
    Wednesday, June 30, 2010 7:11 PM
  • Linq to sql Leandro, es una conjunto de registros de una tabla sql

    Gracias

     

    Wednesday, June 30, 2010 7:16 PM
  • hola

    prueba algo como esto

     

        Public Function LINQToDataTable(Of T)(ByVal varlist As IEnumerable(Of T)) As DataTable
            Dim dtReturn As New DataTable()

            ' column names
            Dim oProps As PropertyInfo() = Nothing

            If varlist Is Nothing Then
                Return dtReturn
            End If

            For Each rec As T In varlist
                ' Use reflection to get property names, to create table, Only first time, others will follow
                If oProps Is Nothing Then
                    oProps = DirectCast(rec.GetType(), Type).GetProperties()
                    For Each pi As PropertyInfo In oProps
                        Dim colType As Type = pi.PropertyType

                        If (colType.IsGenericType) AndAlso (colType.GetGenericTypeDefinition() Is GetType(Nullable(Of ))) Then
                            colType = colType.GetGenericArguments()(0)
                        End If

                        dtReturn.Columns.Add(New DataColumn(pi.Name, colType))
                    Next
                End If

                Dim dr As DataRow = dtReturn.NewRow()

                For Each pi As PropertyInfo In oProps
                    dr(pi.Name) = If(pi.GetValue(rec, Nothing) Is Nothing, DBNull.Value, pi.GetValue(rec, Nothing))
                Next

                dtReturn.Rows.Add(dr)
            Next
            Return dtReturn
        End Function

     

    lo converti a vb.net de este link

    Convert a LINQ Query Resultset to a DataTable

    saludos


    Leandro Tuttini

    Blog
    Buenos Aires
    Argentina
    Wednesday, June 30, 2010 7:37 PM