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.
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
You are trying to avoid the inevitable, I think.
"" , IEnumerable<DataRow>. , DataRow, .
IEnumerable<DataRow>
DataRow
, - DataTable .
DataTable
:
DataTable table; table.BeginLoadData(); foreach (DataRow row in query) { table.ImportRow(row); } table.EndLoadData();