Convert DataTable to List<T>

Kris-I picture Kris-I · Sep 15, 2009 · Viewed 230.6k times · Source

I have an strongly typed DataTable of type MyType, I'd like convert it in a List<MyType>.

How can I do this ?

Thanks.

Answer

Yuriy Faktorovich picture Yuriy Faktorovich · Sep 16, 2009

The following does it in a single line:

dataTable.Rows.OfType<DataRow>()
    .Select(dr => dr.Field<MyType>(columnName)).ToList();

[Edit: Add a reference to System.Data.DataSetExtensions to your project if this does not compile]