Instead of using an ArrayList I would recommend that you use a strongly typed collection, because an ArrayList will not bring much value compared to a non-strongly typed DataTable . Therefore, you can start by defining a model that will represent each row:
public class MyModel { public int Id { get; set; } public string Prop1 { get; set; } public string Prop2 { get; set; } }
then loop around on the DataTable and populate the collection:
List<MyModel> models = new List<MyModel>(); foreach (DataRow row in dt.Rows) { MyModel model = new MyModel { Id = (int)row[0], Prop1 = (string)row[1], Prop2 = (string)row[2] }; models.Add(model); }
Or you can use LINQ if you want:
List<MyModel> models = dt.Rows .Cast<DataRow>() .Select(row => new MyModel { Id = (int)row[0], Prop1 = (string)row[1], Prop2 = (string)row[2] }) .ToList();
Darin Dimitrov
source share