Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From datatable to Entity

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

like image 753
Brian Avatar asked Mar 18 '11 17:03

Brian


2 Answers

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.

like image 72
Adam Flanagan Avatar answered Nov 08 '22 03:11

Adam Flanagan


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

like image 42
Darsin Avatar answered Nov 08 '22 05:11

Darsin