How to convert datatable to generic list in C#?

Views: 291
Created Date: 12-Sep-2016

Use the following code to convert a datatable to a generic list in C#:

[C#]

DataTable dt = new DataTable {

Columns = {
{"Column1", typeof(int)},
{"Column2", typeof(string)}
}
};

for (int i = 0; i < 10000; i++) {
dt.Rows.Add(i, "Row " + i);
}

List lst = new List(dt.Rows.Count);
foreach (DataRow dr in dt.Rows) {
lst.Add(new MyType((int)dr[0], (string)dr[1]));
}

Similar Tags
3.4 357