I want to map data from datatable to custom .NET object. Let's say, I have a .NET object
Public Class MyClass
Public ID
Public Prop1
Public Prop2
End Class
And a corresponding table in the database, which contains columns ID, Prop1 and Prop2.
I want to generate a list of .NET objects from the database. Currently I fill the dataset and map each property individually. Is there any way, to map the database automatically, so the properties would map according to property / column name.
'retreive dataset from db'
DaAdapter.Fill(ds)
'create a new list of my object'
Private l As New List(Of MyClass)
'iterate through dataset (this is the step I want to get rid of - of course'
'it could only work if the property of the MyClass and the column in '
'the database have the same name)'
For Each r As DataRow in ds.Tables(0).Rows
Dim itm As New MyClass
itm.ID = r("ID")
itm.Prop1 = r("Prop1")
itm.Prop2 = r("Prop2")
l.Add(itm)
Next