我想将数据从数据表映射到自定义 .NET 对象。假设我有一个 .NET 对象
Public Class MyClass
Public ID
Public Prop1
Public Prop2
End Class
以及数据库中对应的表,其中包含列 ID、Prop1 和 Prop2。
我想从数据库中生成一个 .NET 对象列表。目前我填充数据集并单独映射每个属性。有没有办法自动映射数据库,所以属性会根据属性/列名进行映射。
'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