Datatable to List of objects mapper.

In my previous blog post about mapping data rows to objects you saw how easy it was to use reflection to map a data row to an object. With a little extra effort you can map data tables to a list of those mapped objects. The code below builds on my previous blog and will not work unless you have the code from that blog entry.

Public Shared Function MapDataTableToList(Of T)(ByVal dt As DataTable) As IList(Of T)
        If dt Is Nothing Then
            Return Nothing
        End If
 
        Dim list As IList(Of T) = New List(Of T)
       
        For Each dr As DataRow In dt.Rows
            list.Add(MapDatarowToObject(Of T)(dr))
        Next
 
        Return list
    End Function
To explain how this works we’ll go line by line

Public Shared Function MapDataTableToList(Of T)(ByVal dt As DataTable) As IList(Of T)

I’m creating a method that needs to be called by defining a type for T. The only parameter is a data table. An example of calling this method looks like this:

Dim dt As DataTable = GetDataTableFromDb()
Dim usersList As IList(Of User) = MapDataTableToList(Of User)(dt)
 
This would convert a data table to a generic list (IList) of user objects.

If dt Is Nothing Then
Return Nothing
End If

These lines of code make sure that the data table parameter has a value.

Dim list As IList(Of T) = New List(Of T)
 
Create a generic list of objects based on T. T is the generic type specified when calling the method. In the example usage above the T parameter would be of type User.
 
 
For Each dr As DataRow In dt.Rows
list.Add(MapDatarowToObject(Of T)(dr))
Next

Iterate through each data row in the data table and call MapDatarowToObject (from the previous blog entry) and add the mapped object to the list.


Return list
 
Return the list of objects from the method.

So there you go, An easy to use data table to object mapper.

Until next time.

posted @ Wednesday, July 30, 2008 8:44 AM

Print

Comments on this entry:

# re: Datatable to List of objects mapper.

Left by Bill at 7/31/2008 2:03 PM
Gravatar
Thanks for this Chris. Great compliment to your previous article.

# re: Datatable to List of objects mapper.

Left by George at 8/26/2008 7:24 PM
Gravatar
For Each dr As DataRow In dt.Rows
list.Add(MapDatarowToObject(Of T)(dr))
Next
'would it be better if
dim dr as DataRow
'then
For Each dr In dt.Rows
list.Add(MapDatarowToObject(Of T)(dr))
Next

# re: Datatable to List of objects mapper.

Left by dauchande at 8/31/2008 1:04 AM
Gravatar
Uhm, didn't NHibernate 2.0 just ship?

# re: Datatable to List of objects mapper.

Left by Mayuresh at 9/4/2008 12:07 PM
Gravatar
Can you send the C# version of this

# re: Datatable to List of objects mapper.

Left by Hungster at 10/3/2009 9:44 AM
Gravatar
Thanks! I was planning to implement something similar until I found your previous article and this one. This works like a charm.

# Post on DataTable to List Of Mapper

Left by Fred Mastro at 12/8/2009 5:44 PM
Gravatar
Just wanted to say this is a great post and I used it for a need I had. Though I had a problem with casting database strings to unknown, until run-time, properties. Hope you don't mind I referenced your site on my blog and put up a copy of some of the code you had in the two posts. I linked back to the original.
You can see it here, http://www.fredmastro.com/post/Mapping-a-DataTable-to-an-Object-Class-Like-a-homemade-Linq-to-SQL-Send-over-WCF-to-SilverLight.aspx

# re: Datatable to List of objects mapper.

Left by Write Thesis Proposal at 12/30/2009 3:53 PM
Gravatar
I must admit, great stuff!

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 6 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910