简体   繁体   English

无法使用datatable.select复制datatable

[英]Unable to copy datatable with datatable.select

I want to create a copy of a datatable. 我想创建一个数据表的副本。 I am not sure what is the correct way to do it. 我不确定什么是正确的方法。 So, I decided to use 所以,我决定使用

DataTable filteredData = sourceDataTable.Select(expression).CopyToDataTable();

as mentioned in this post - How to pass DataTable.Select() result to a new DataTable? 如本文所述- 如何将DataTable.Select()结果传递给新的DataTable?

I tried to use it by setting expression = "where 'TRUE' = 'TRUE'". 我试图通过设置expression =“ where'TRUE'='TRUE'”来使用它。 I hoped that this will not filter any rows from original data table, ie full copy. 我希望这不会过滤原始数据表中的任何行,即完整副本。 But, I got the error - System.Data.SyntaxErrorException: Syntax error: Missing operand after ''TRUE'' operator. 但是,我得到了错误-System.Data.SyntaxErrorException:语法错误:在“ TRUE”运算符后缺少操作数。

How do I copy a datatable easily ? 如何轻松复制数据表?

You can use the DataTable.Copy method. 您可以使用DataTable.Copy方法。

Copy creates a new DataTable with the same structure and data as the original DataTable. 复制将创建一个新的DataTable,其结构和数据与原始DataTable相同。

var copiedDataTable = sourceDataTable.Copy();

As from Winney response you could use DataTable.Copy (and probably is the best option in your case), but if you still want to use Select then don't pass any filter expression 从Winney响应开始,您可以使用DataTable.Copy(这可能是您的最佳选择),但是如果您仍然想使用Select则不要传递任何过滤器表达式

 DataTable filteredData = sourceDataTable.Select().CopyToDataTable();

DataTable.Select has an overload that doesn't accept any parameter and thus select everything is present in the DataTable DataTable.Select具有不接受任何参数的重载,因此选择DataTable中存在的所有内容

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

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