Trojan.ZBOT
Trojan.ZBOT

Reputation: 1488

Copy selected data from DataTable to another DataTable

I want to select some of the data from a datatable and copy it into another data table. I found the answer in one question - How to query a DataTable in memory to fill another data table

DataTable table = GetDataTableResults();
DataTable results = table.Select("SomeIntColumn > 0").CopyToDataTable();

This code does not work and causes the error -

'System.Array' does not contain a definition for 'CopyToDataTable' and 
no extension method 'CopyToDataTable' accepting a first argument of type 
'System.Array' could be found (are you missing a using directive or 
an assembly reference?)

I am using visual studio 2008 and .net 3.5.

Upvotes: 2

Views: 19835

Answers (2)

gpmurthy
gpmurthy

Reputation: 2427

Make sure you have the right references. In this you need System.Data.DataSetExtensions Assembly and System.Data Namespace. The following link should help...

http://msdn.microsoft.com/en-us/library/bb396189(v=vs.110).aspx

Good Luck!

EDIT: Screenshot of Reference...

enter image description here

Upvotes: 4

Karl Anderson
Karl Anderson

Reputation: 34846

You can always just loop through the array of DataRow objects returned from Select() and import them into the new data table, like this:

DataTable table = GetDataTableResults();
DataTable results;

DataRow[] rowArray = table.Select("SomeIntColumn > 0");

foreach (DataRow row in rowArray) 
{
    results.ImportRow(row);
}

Upvotes: 1

Related Questions