开发者

From datatable to Entity

开发者 https://www.devze.com 2023-02-17 13:33 出处:网络
Is it p开发者_如何学Goossible to populate an entity with the contents of a DataTable?I\'m not sure this is exactly what you\'re looking for but it should work; there is a .AsEnumerable() extension met

Is it p开发者_如何学Goossible to populate an entity with the contents of a DataTable?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消