简体   繁体   中英

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?

I tried to use it by setting 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.

How do I copy a datatable easily ?

You can use the DataTable.Copy method.

Copy creates a new DataTable with the same structure and data as the original 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

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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