Convert DataTable to a Generic List of Class with vb.net

Good Morning. My daughter continues her failure to follow rules this weekend. She claimed the roads were so bad she could not return home last night even though there was only a dusting of snow. In other news, only 17 days till my trip to the Dominican. So that is something to look forward to.

Today’s topic is how to convert a datatable to a Generic list of any class. I am doing something with my new plugin project for Windows Live Writer. I used simple silly names to be clear what I am doing in this sample. It isn’t perfect but it works. Anyway, make it a great day!


Join me on Facebook

It’s usage is as below and the code follows:

Protected Function GetListFromTable(ByVal Table As DataTable, ByVal DummyObj As ICopy, ByVal ObjList As IList) As IList
        For Each dr As DataRow In Table.Rows
            ObjList.Add(DummyObj.NewCopy(dr.ItemArray))
        Next
        Return ObjList
    End Function

Public Interface ICopy
    Function NewCopy(ByVal params As Object()) As ICopy
End Interface
Public Class Form1
    Dim ElephantList As New List(Of elephant)
    Dim GiraffeCollection As New Collection
    Private Class elephant
        Implements ICopy
        Private _legs As Integer
        Private _trunk As Boolean
        Public Property Legs() As Integer
            Get
                Return _legs
            End Get
            Set(ByVal value As Integer)
                _legs = value
            End Set
        End Property
        Public Property Trunk() As Boolean
            Get
                Return _trunk
            End Get
            Set(ByVal value As Boolean)
                _trunk = False
            End Set
        End Property
        Public Sub New()
        End Sub
        Public Sub New(ByVal legs As Integer, ByVal trunk As Boolean)
            _legs = legs
            _trunk = trunk
        End Sub
        Public Function NewCopy(ByVal params As Object()) As ICopy Implements ICopy.NewCopy
            Return New elephant(params(0), params(1))
        End Function
    End Class
    Private Class giraffe
        Implements ICopy
        Private _legs As Integer
        Private _neck As String
        Private _tail As Boolean
        Public Property Legs() As Integer
            Get
                Return _legs
            End Get
            Set(ByVal value As Integer)
                _legs = value
            End Set
        End Property
        Public Property Neck() As String
            Get
                Return _neck
            End Get
            Set(ByVal value As String)
                _neck = False
            End Set
        End Property
        Public Property Tail() As Boolean
            Get
                Return _tail
            End Get
            Set(ByVal value As Boolean)
                _tail = value
            End Set
        End Property
        Public Sub New()
        End Sub
        Public Sub New(ByVal legs As Integer, ByVal neck As String, ByVal tail As Boolean)
            _legs = legs
            _neck = neck
            _tail = tail
        End Sub
        Public Function NewCopy(ByVal params As Object()) As ICopy Implements ICopy.NewCopy
            Return New giraffe(params(0), params(1), params(2))
        End Function
    End Class
    Private ElephantTable As New DataTable
    Private GiraffeTable As New DataTable
    Private Sub makeElephantTable()
        Dim dc0 As New DataColumn("Legs", GetType(Integer))
        ElephantTable.Columns.Add(dc0)
        Dim dc1 As New DataColumn("Trunk", GetType(Boolean))
        ElephantTable.Columns.Add(dc1)
        For i As Integer = 0 To 5
            ElephantTable.Rows.Add(i, i Mod 2 = 0)
        Next
        ElephantTable.AcceptChanges()
    End Sub
    Private Sub makeGiraffeTable()
        Dim dc0 As New DataColumn("Legs", GetType(Integer))
        GiraffeTable.Columns.Add(dc0)
        Dim dc1 As New DataColumn("Neck", GetType(String))
        GiraffeTable.Columns.Add(dc1)
        Dim dc2 As New DataColumn("Tail", GetType(Boolean))
        GiraffeTable.Columns.Add(dc2)
        For i As Integer = 0 To 5
            GiraffeTable.Rows.Add(i, "Long", i Mod 2 = 0)
        Next
        GiraffeTable.AcceptChanges()
    End Sub
    Protected Function GetListFromTable(ByVal Table As DataTable, ByVal DummyObj As ICopy, ByVal ObjList As IList) As IList
        For Each dr As DataRow In Table.Rows
            ObjList.Add(DummyObj.NewCopy(dr.ItemArray))
        Next
        Return ObjList
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        makeElephantTable()
        ElephantList = GetListFromTable(ElephantTable, New elephant, ElephantList)
        For Each item As elephant In ElephantList
            Debug.WriteLine(item.Legs.ToString & "|" & item.Trunk.ToString)
        Next
        makeGiraffeTable()
        GiraffeCollection = GetListFromTable(GiraffeTable, New giraffe, GiraffeCollection)
        For Each item As giraffe In GiraffeCollection
            Debug.WriteLine(item.Legs.ToString & "|" & item.Neck.ToString & "|" & item.Tail.ToString)
        Next
    End Sub
End Class


Join me on Facebook

Technorati Tags: ,,,,,,,,,,,,,,,,,,,,,

  1. #1 by Grant on May 5, 2010 - 7:00 am

    Thanks for this code. It really has improved my loading time of my screens.

Leave a comment