Automapper with datatable

379 Views Asked by At

Task : I need to map the records of datatable which contains same column name as CLass Person properties into List of Person class object .

For this "Person" class have FN and LN properties . and datatable contains "FN" and "LN" column name, Now records of datatable needs to map into List of "Person" class

Now the below, code is using for creating dynamic mapping by automapper 4.0 , So now problem is that when I am applying AutoMapper.Mapper.DynamicMap Dim people As List(Of Person) = AutoMapper.Mapper.DynamicMap(Of IDataReader, List(Of Person))(table.CreateDataReader()) it returning me count 0 in "people" object .

Code :

/ Create class /

Public Class Person
  Public Property FN As String
  Public Property LN As String
End Class

/ Create datatable

     Dim table = New DataTable()
     table.Columns.Add("FN", GetType(String))
    table.Columns.Add("LN", GetType(String))
    table.Rows.Add("Jeff", "Barnes")
    table.Rows.Add("George", "Costanza")
    table.Rows.Add("Stewie", "Griffin")
    table.Rows.Add("Stan", "Marsh")
    table.Rows.Add("Eric", "Cartman")

/ Need to map records of datatable into Person class properties .

Dim people As List(Of Person)=AutoMapper.Mapper.DynamicMap(Of IDataReader,List(Of Person))(table.CreateDataReader())

**/ But issue is that, I am getting 0 count of people list, Am I missing some thing in this code . ****

0

There are 0 best solutions below