简体   繁体   English

从数据表中选择特定的列并将其复制到另一个数据表

[英]Selecting specific columns from a dataTable and copy it to another DataTable

I have a DataTable Dt1 and another DataTable Dt2. 我有一个DataTable Dt1和另一个DataTable Dt2。

Dt1 contains many columns and rows and Dt2 is basically empty Dt1包含许多列和行,而Dt2基本为空

I know I should use Dt1.Select to select the specific DataTaRow [] but how can I copy them to Dt2 我知道我应该使用Dt1.Select来选择特定的DataTaRow [],但是如何将它们复制到Dt2

DataRow [] row = Dt1.Select("ID,MIN_VALUE,MAX_VALUE");

how can i copy them to Dt2? 我如何将它们复制到Dt2?

For .Net Framework 3.5+ 对于.Net Framework 3.5+

You can use CopyToDataTable method. 您可以使用CopyToDataTable方法。

Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable object. 给定输入IEnumerable对象,返回一个包含DataRow对象副本的DataTable。

DataTable dt1 = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt2 = dr.CopyToDataTable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM