如何将类型化的数据表转换为实体列表?

如何将类型化的数据表转换为实体列表?

问题描述:

我需要将类型化数据表的数据转换为我的实体列表。我已经用DataTable的字段映射了实体的所有字段。

I need to convert the data of a Typed DataTable to List of my entity. I've Mapped all the fields of entity with field of DataTable.

有什么想法吗?

一种方法是通过自定义代码
进行操作,假设您的实体类名称为'MyEnt'

One way could be by doing it through custom code Lets suppose you Entity class name is 'MyEnt'

 public class MyEnt
    {
     public string Name {get; set;}
     public string Type {get; set;}
     public LoadMyEnt(object [] array)
      {
         this.Name = array[0].ToString();
         this.Type = array[1].ToString();   
      }
    }

//对于数据表,您可以这样做

//for datatable, you could do

List<MyEnt> list = new List<MyEnt>();
foreach(DataRow dr in table.Rows)
{
  MyEnt obj = new MyEnt();
obj.Load(dr.ItemArray);
list.Add(obj);
}