List<Locations> locations = new List<Locations>();
DataTableReader reader = dt.CreateDataReader();
if (reader != null && reader.HasRows)
{
while (reader.Read())
{
locations.Add(new Locations()
{
id = reader.GetInt32("id"),
...
});
}
}
I wrote a bunch of extension methods for the DataTableReader that allows me to assign default values in case something came back wonky (yes, my data environment may produce wonky results, even from the database). I also access the reader by the column's ordinal value instead of by name so I can verify that the desired column actually exists in the table. An example:
public static Int32 GetInt32OrDefault(this DataTableReader reader, int ordinal, Int32 defaultValue)
{
Int32 value = defaultValue;
if (!reader.IsDBNull(ordinal))
{
value = reader.GetInt32(ordinal);
}
return value;
}