none
Reflection - Convert List to DataTable RRS feed

  • Question

  • I have two classes (nested) as below:

    Public Class CRMSourceTableInfo
        Public crm_source_table As String
        Public crm_source_table_id As Integer
        Public subClass As CRMSourceTableInfoSubClass
    End Class

    Public Class CRMSourceTableInfoSubClass
        Public sub1 As String
    End Class

    and my code as below:

    Dim lst3 As New List(Of CRMSourceTableInfo)
    lst3.Add(New CRMSourceTableInfo With {.crm_source_table = "cp",.crm_source_table_id = 100})
    lst3.Add(New CRMSourceTableInfo With {.crm_source_table = "cpa", .crm_source_table_id = 101, .subClass = New CRMSourceTableInfoSubClass With {.sub1 = "something"}})

    Now, using Reflection, I would like to convert the "lst3" (List) to a dataTable without the sub class "CRMSourceTableInfoSubClass".

    Pl. help!

    Monday, August 12, 2013 6:59 PM

Answers

  • Hi Ganesh,

    Please check the below code

    Imports System.Data
    Imports System.Reflection
    
    Module Module1
        Sub Main()
            Dim col As New CollectionToDataTable()
            Dim oDt As DataTable = col.ConvertToDataTable()
            Console.WriteLine(oDt.Rows.Count)
        End Sub
    
        Public Class CollectionToDataTable
            Public Function ConvertToDataTable() As DataTable
                Dim oDataTable As New DataTable
                Try
                    Dim lst3 As New List(Of CRMSourceTableInfo)
                    lst3.Add(New CRMSourceTableInfo With {.crm_source_table = "cp", .crm_source_table_id = 100})
                    lst3.Add(New CRMSourceTableInfo With {.crm_source_table = "cpa", .crm_source_table_id = 101, .subClass = New CRMSourceTableInfoSubClass With {.sub1 = "something"}})
    
                    Dim listOfFields As List(Of FieldInfo) = lst3(0).GetType().GetFields().ToList()
    
                    listOfFields = listOfFields.Where(Function(item) item.Name <> "subClass").ToList()
    
                    For idx As Integer = 0 To listOfFields.Count - 1
                        oDataTable.Columns.Add(listOfFields(idx).Name)
                    Next
                    Dim oDataRow As DataRow
                    For rowCnt As Integer = 0 To lst3.Count - 1
                        oDataRow = oDataTable.NewRow()
                        For idx As Integer = 0 To listOfFields.Count - 1
                            Dim tmp As Object = lst3(rowCnt)
                            Dim tName As Object = tmp.GetType().InvokeMember(listOfFields(idx).Name, BindingFlags.GetField, Nothing, tmp, New Object() {})
                            If Not tName Is Nothing Then
                                oDataRow(idx) = tName.ToString()
                            End If
                        Next
                        oDataTable.Rows.Add(oDataRow)
                    Next
                Catch ex As Exception
                    Throw
                End Try
                Return oDataTable
            End Function
        End Class
    
        Public Class CRMSourceTableInfo
            Public crm_source_table As String
            Public crm_source_table_id As Integer
            Public subClass As CRMSourceTableInfoSubClass
        End Class
    
        Public Class CRMSourceTableInfoSubClass
            Public sub1 As String
        End Class
    End Module
    


    Thanks
    Subhendu DE - Visit My Blog If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Tuesday, August 13, 2013 5:16 PM

All replies

  • Hi Ganesh,

    I have already written one blog about "How to Convert Business Entity Collection to DataTable"

    Please check this and customize as per your need

    http://subhendude.wordpress.com/2009/07/02/how-to-convert-business-entity-collection-to-datatable/

    Please let me know if you need further help.


    Thanks
    Subhendu DE - Visit My Blog If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Tuesday, August 13, 2013 4:48 AM
  • Subhendu,

    Thank you for the link to your blog. It does address the basic "property" handling but it does not address "fields" and also does not address nesting of classes.

    Could you help?

    Tuesday, August 13, 2013 1:52 PM
  • Hi Ganesh,

    Please check the below code

    Imports System.Data
    Imports System.Reflection
    
    Module Module1
        Sub Main()
            Dim col As New CollectionToDataTable()
            Dim oDt As DataTable = col.ConvertToDataTable()
            Console.WriteLine(oDt.Rows.Count)
        End Sub
    
        Public Class CollectionToDataTable
            Public Function ConvertToDataTable() As DataTable
                Dim oDataTable As New DataTable
                Try
                    Dim lst3 As New List(Of CRMSourceTableInfo)
                    lst3.Add(New CRMSourceTableInfo With {.crm_source_table = "cp", .crm_source_table_id = 100})
                    lst3.Add(New CRMSourceTableInfo With {.crm_source_table = "cpa", .crm_source_table_id = 101, .subClass = New CRMSourceTableInfoSubClass With {.sub1 = "something"}})
    
                    Dim listOfFields As List(Of FieldInfo) = lst3(0).GetType().GetFields().ToList()
    
                    listOfFields = listOfFields.Where(Function(item) item.Name <> "subClass").ToList()
    
                    For idx As Integer = 0 To listOfFields.Count - 1
                        oDataTable.Columns.Add(listOfFields(idx).Name)
                    Next
                    Dim oDataRow As DataRow
                    For rowCnt As Integer = 0 To lst3.Count - 1
                        oDataRow = oDataTable.NewRow()
                        For idx As Integer = 0 To listOfFields.Count - 1
                            Dim tmp As Object = lst3(rowCnt)
                            Dim tName As Object = tmp.GetType().InvokeMember(listOfFields(idx).Name, BindingFlags.GetField, Nothing, tmp, New Object() {})
                            If Not tName Is Nothing Then
                                oDataRow(idx) = tName.ToString()
                            End If
                        Next
                        oDataTable.Rows.Add(oDataRow)
                    Next
                Catch ex As Exception
                    Throw
                End Try
                Return oDataTable
            End Function
        End Class
    
        Public Class CRMSourceTableInfo
            Public crm_source_table As String
            Public crm_source_table_id As Integer
            Public subClass As CRMSourceTableInfoSubClass
        End Class
    
        Public Class CRMSourceTableInfoSubClass
            Public sub1 As String
        End Class
    End Module
    


    Thanks
    Subhendu DE - Visit My Blog If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.

    Tuesday, August 13, 2013 5:16 PM