简体   繁体   中英

Convert LINQ to DataTable Without Use CopyToDataTable

您好我尝试使用CopyToDataTable但没有Visual Studio 2010和2015的每个方法,请帮助我如何在不使用CopyToDataTable的情况下将Linq查询转换为DataTable或将其逐步发送给我以添加此方法

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.

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