I have an strongly typed DataTable of type MyType
, I'd like convert it in a List<MyType>
.
How can I do this ?
Thanks.
In the ADO.NET library, C# DataTable is a central object. It represents the database tables that provide a collection of rows and columns in grid form. There are different ways to create rows and columns in the DataTable.
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]
List<MyType> listName = dataTableName.AsEnumerable().Select(m => new MyType() { ID = m.Field<string>("ID"), Description = m.Field<string>("Description"), Balance = m.Field<double>("Balance"), }).ToList()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With