I have something like the following C# Model:
public class ExampleModel
{
public string A {get; set;}
public string B {get; set;}
//...
}
And the following SQL code to get the rows of a database table:
SELECT a, b, ...
FROM example_table;
I want to somehow map the result of that SQL query to a list of ExampleModel. What I've been doing so far is something like this:
using (DbCommand cmd = _conn.CreateCommand())
{
string sql = "SELECT a, b, ... FROM example_table;";
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = sql;
using(DbDataReader reader = cmd.ExecuteReader())
{
List<ExampleModel> exampleModelList = new();
while(reader.Read())
{
ExampleModel exampleModel = new ExampleModel();
exampleModel.A = (string)reader["a"];
exampleModel.B = (string)reader["b"];
//...
exampleModelList.Add(exampleModel);
}
}
}
Assume that my database has a very high amount of columns. Is there a quicker way to do what I'm trying to do without having to manually map data in table rows to my C# object?
Note: I can't use Entity Framework to do this.