问题
I went through How can I easily convert DataReader to List<T>?
I wanted to implement something like what is accepted as an answer in the above link.
Scenrio:
I am using OdbcDataReader to retrieve from the database.
And I have a Model Class . FYI , the properties of this class are exact replica of the column names from the database. I need to map these columns to the properties and return List Can this be accomplished using Automapper.
回答1:
Something like this
public List<T> ReadData<T>(string queryString)
{
using (var connection = new SqlConnection(constr))
using (var command = new SqlCommand(queryString, connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
if (reader.HasRows)
return Mapper.DynamicMap<IDataReader, List<T>>(reader);
}
return null;
}
Define your class
public class MarkType
{
public int id { get; set; }
public string name { get; set; }
public DateTime inserted { get; set; }
}
Use
List<MarkType> lst = _helper.ReadData<MarkType>("SELECT [id],[name],[inserted] FROM [marktype]");
来源:https://stackoverflow.com/questions/25681102/using-automapper-with-data-reader