如何使用Linq将一个数据表复制到另一个数据表. [英] How to copy one datatable to another datatable using Linq.

查看:125
本文介绍了如何使用Linq将一个数据表复制到另一个数据表.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个包含员工信息的数据表,如
EmpID,EmpName,EmpAdd等...
在我的数据表中,EmpName和EmpAdd已加密.因此,当我将其绑定到网格时,我想将其转换为字符串.
例如.

Hi,
I have one datatable which contain Employee Information like
EmpID,EmpName,EmpAdd etc...
In my datatable EmpName and EmpAdd is Encrypted. So when i bind it to grid that time i want to convert it to string.
for Ex.

DataTable dt = new DataTable();
foreach (DataRow item in dsReturn.Tables[0].Rows)
{
     DataRow dr = dt.NewRow();
     dr["EmpID"] = item["EmpID"];
     dr["EmpName"] = ConverterByteToString((byte[])item["EmpName"]);
     dr["EmpAdd"] = ConverterByteToString((byte[])item["EmpAdd"]);
     dt.Rows.Add(dr);
}



是否可以使用Linq将所有行复制到新表中?

这可以正常工作,但是当大量的数据处理时间变慢时.因此,我想简化它.



Is there any way to copy all row into new table using Linq?

This work fine but when large amount of data procedure that time it''s slow. So i want to simplify it.

推荐答案

表常规具有^ ]方法,您可以用来一次添加多个对象,例如:
Table generic has a InsertAllOnSubmit[^] method, you can use to add multiple objects at one, like this:
// dc = DataContext, assumes TableA contains items of type A
var toInsert = from b in TableB
               where ...
               select new A { ... };

TableA.InsertAllOnSubmit(toInsert);
dc.SubmitChanges();




or

db
  .TableA
  .InsertAllOnSubmit(
    db
      .TableB
      .Where( ... )
      .Select(b => new A { ... })
  );


但是 即使延迟执行,每条受影响的记录也会从SQL Server一直到应用程序再返回.您最好寻找服务器端解决方案.如果内置工具不够用,则可以创建.net集成功能和过程并将其添加到服务器和数据库中.这样,数据将不会被序列化,也不会传输,并且您将获得很多性能.如果决定采用此方法,则可以从这里开始: http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.100).aspx [


But even with deferred execution, every affected record will go all the way from the SQL server to the application and back. You better look for a server side solution. If the built-in tools are not enough, you can create .net integrated functions and procedures and add it to the server and database. This way, the data will not be serialized, will not travel, and you gain a lot of performance. If you decide to take this path, you can start here: http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.100).aspx[^]


这篇关于如何使用Linq将一个数据表复制到另一个数据表.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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