Entity from datatable

c# datatable entity-framework

Question

Is it possible to populate an entity with the contents of a DataTable?

1
6
3/18/2011 5:01:49 PM

Accepted Answer

I'm not sure this is exactly what you're looking for but it should work; there is a .AsEnumerable() extension method which you can then use to project the row 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 doesn't do any type conversion so if the column hasn't had the type set you will need to do the conversion yourself.

10
3/18/2011 5:30:38 PM

Popular Answer

I believe this is what you are looking for: Converting DataTable to Entities



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow