如何将类型化的数据表转换为实体列表? [英] How to convert typed DataTable into List of Entity?

查看:120
本文介绍了如何将类型化的数据表转换为实体列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将类型化数据表的数据转换为我的实体列表。我已经用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);
}

这篇关于如何将类型化的数据表转换为实体列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆