将数据表转换为实体 [英] Convert datatables into the entities

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

问题描述

我是.net的新手,我在一家公司实习.
要完成我的任务,我需要将数据表转换为实体.

Hi I am new to .net and I got an internship in a company.
To complete my task I need to convert the datatable into the entity.

Can any one please give me code so that I can convert the datatable into the entity?

推荐答案

如果用实体"表示相关类的实例,那么抱歉-在不了解您需要创建的类以及数据表格式的更多信息的情况下,确实不可能如此具体地给您提供代码!
If by "entity" you mean instances of the relevant class, then sorry - without knowing a lot more about the class you need to create, and the format of your datatable it is really not possible to be so specific as to give you code!


尝试一下,

您可以使用自己的属性声明自己的类,并从Datatable中加载它的值,例如,

Try this,

You can Declare your own class with your own properties and load it''s values from your Datatable like this,

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);
}



希望对您有所帮助.



I hope this will help you.


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

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