Convert LINQ to DataTable Without Use CopyToDataTable

Hello Every one I'm Tried To Use CopyToDataTable But Don't have my Visual Studio 2010 And 2015 this Method Please Help me How Convert Linq Query To DataTable With out Use CopyToDataTable Or Send me step by Step to Add This Method

Upvotes: 0

Views: 128

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460288

You can use originalTable.Clone() and a loop with DataTable.ImportRow:

DataTable tableResult = originalTable.Clone(); // empty
foreach(DataRow row in yourLinqQuery)
    tableResult.ImportRow(row);

This has also one advantage over CopyToDataTable: it doesn't cause an InvalidOperationException if the query doesn't return any rows. With this approach the table will be empty.

Upvotes: 1

Related Questions