如何将DataTable转换为类Object [英] How to convert DataTable to class Object

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

问题描述

我已经开发了一个返回DataTable的应用程序。



现在我的客户端想转换(使用某些部分使用服务堆栈),所以我需要返回DTO (对象)在我的应用程序。



我不想更改我现有的存储过程,甚至不想尽可能多地使用LINQ(我不是太多感谢LINQ)



对于小功能,我可以使用Linq没有问题。



我的问题是:如何将我的 DataTable 更改为该类的对象?



示例代码位于

  string s = DateTime.Now.ToString(); 
DataTable dt = new DataTable();

dt.Columns.Add(id);
dt.Columns.Add(name); (int i = 0; i< 5000000; i ++)


{
DataRow dr = dt.NewRow();
dr [id] = i.ToString();
dr [name] =name+ i.ToString();
dt.Rows.Add(dr);

dt.AcceptChanges();
}

列表< Class1> clslist = new List< Class1>(); (int i = 0; i
Class1 cls = new Class1();
cls.id = dt.Rows [i] [id]。ToString();
cls.name = dt.Rows [i] [name]。ToString();
clslist.Add(cls);
}

Response.Write(s);
Response.Write(< br>);
Response.Write(DateTime.Now.ToString());

我知道,上面是耗时的,所以我试图找到一个替代解决方案。 p>

有没有任何替代方法(我猜,LINQ to DataTable)通过它直接将表的行转换为 List< Class1>



所以我可以在我的服务堆栈中返回对象,然后继续。

解决方案

初始化DataTable:

  DataTable dt = new DataTable(); 
dt.Columns.Add(id,typeof(String));
dt.Columns.Add(name,typeof(String)); (int i = 0; i <5; i ++)
{
string index = i.ToString();
dt.Rows.Add(new object [] {index,name+ index});
}

查询本身:

  IList< Class1> items = dt.AsEnumerable()。选择(row => 
new Class1
{
id = row.Field< string>(id),
name = row .Field< string>(name)
})ToList();


I have already develop an application which returns DataTable everywhere.

Now my client wants to convert (use some part using service stack), so I need to return DTO (objects) in my application.

I don't want to change my existing stored procedures or even not want to use LINQ as much as possible (I am not too much aware with LINQ).

For small functionality, I can use Linq no issue.

My question is: how can I change my DataTable to objects of that class?

The sample code is below

string s = DateTime.Now.ToString();
DataTable dt = new DataTable();

dt.Columns.Add("id");
dt.Columns.Add("name");

for (int i = 0; i < 5000000; i++)
{
    DataRow dr = dt.NewRow();
    dr["id"] = i.ToString();
    dr["name"] = "name" + i.ToString();
    dt.Rows.Add(dr);

    dt.AcceptChanges();
}

List<Class1> clslist = new List<Class1>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    Class1 cls = new Class1();
    cls.id = dt.Rows[i]["id"].ToString();
    cls.name = dt.Rows[i]["name"].ToString();
    clslist.Add(cls);
}

Response.Write(s);
Response.Write("<br>");
Response.Write(DateTime.Now.ToString());

I know, above is time consuming, and so I am trying to find an alternate solution.

Is there any alternative way (I guess, LINQ to DataTable) by which it directly converts the rows of tables to List<Class1>?

So I can return objects in my service stack and go ahead.

解决方案

Initialize DataTable:

DataTable dt = new DataTable(); 
dt.Columns.Add("id", typeof(String)); 
dt.Columns.Add("name", typeof(String)); 
for (int i = 0; i < 5; i++)
{
    string index = i.ToString();
    dt.Rows.Add(new object[] { index, "name" + index });
}

Query itself:

IList<Class1> items = dt.AsEnumerable().Select(row => 
    new Class1
        {
            id = row.Field<string>("id"),
            name = row.Field<string>("name")
        }).ToList();

这篇关于如何将DataTable转换为类Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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