From datatable to Entity

Can I populate an object with the contents of a DataTable?

+6
c # datatable entity-framework
source share
2 answers

I'm not sure if this is exactly what you are looking for, but it should work; exists . The extension method AsEnumerable () , which can then be used to project a string into a new entity.

var products = productTable.AsEnumerable().Select(row => new Product { ProductID = row.Field<int>("ProductID"), Name = row.Field<string>("Name"), CreatedDate = row.Field<DateTime>("CreatedDate") }); 

As far as I know, the .Field<T>() method does not perform any type conversion, so if there was no type set in the column, you will need to do the conversion yourself.

+9
source share

I believe this is what you are looking for: Convert DataTable to Objects

+1
source share

All Articles