How to assign a collection of strings (obtained using linq query) to datatable without using a loop?

I have a scenario where a datatable can contain a large number of rows. As a result, I cannot repeat and update data using a loop.

I use the following code to get a collection of strings,

         from row in CSVDataTable.AsEnumerable()
         where CSVDataTable.Columns.Cast<DataColumn>().Any(col => !row.IsNull(col))
         select row;

Someone please tell me how to assign the result of the above code to datatable without using a loop.

+3
source share
2 answers

I can assign the result of a Linq query to a data table with the following code:

           // Create a DataTable from Linq query.       

             IEnumerable<DataRow> query = from row in CSVDataTable.AsEnumerable()
                                            where CSVDataTable.Columns.Cast<DataColumn>().Any(col => !row.IsNull(col))
                                            select row; //returns IEnumerable<DataRow>

             DataTable CSVDataTableWithoutEmptyRow = query.CopyToDataTable<DataRow>();

See link for more details.

http://msdn.microsoft.com/en-us/library/bb386921.aspx

+4
source

You are trying to avoid the inevitable, I think.

"" , IEnumerable<DataRow>. , DataRow, .

, - DataTable .

:

DataTable table;
table.BeginLoadData();
foreach (DataRow row in query)
{
     table.ImportRow(row);
}
table.EndLoadData();
+1

All Articles