DataTableExtensions.CopyToDataTable(Of T) Method (IEnumerable(Of T))
Assembly: System.Data.DataSetExtensions (in System.Data.DataSetExtensions.dll)
'Declaration <ExtensionAttribute> _ Public Shared Function CopyToDataTable(Of T As DataRow) ( _ source As IEnumerable(Of T) _ ) As DataTable
The type of objects in the source sequence, typically DataRow.
Return ValueType: System.Data.DataTable
A DataTable that contains the input sequence as the type of DataRow objects.
Usage NoteIn Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable(Of ). When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
The source IEnumerable(Of T) sequence is Nothing and a new table cannot be created.
The source sequence does not contain any DataRow objects.
A DataRow in the source sequence is Nothing.
The input sequence can be any IEnumerable(Of T) source, not only a query or a query over a DataTable. Note that if the source sequence is a query, calling this operator will force immediate execution. Enumerating the source sequence can also cause exceptions to be thrown.
The schema of the destination table is based on the schema of the first DataRow row in the source sequence. The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. For a typed DataTable, types are not preserved. The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.
The generated table returned by the method has the default table name. If you want to name the DataTable, attach it to a DataSet, or perform any other DataTable specific operation, you must do so after the table is created and returned.
For more information, see Creating a DataTable From a Query (LINQ to DataSet).
The following example queries the SalesOrderHeader table for orders after August 8, 2001, and uses the CopyToDataTable method to create a DataTable from that query. The DataTable is then bound to a BindingSource, which acts as proxy for a DataGridView.
' Bind the System.Windows.Forms.DataGridView object ' to the System.Windows.Forms.BindingSource object. dataGridView.DataSource = bindingSource ' Fill the DataSet. Dim ds As New DataSet() ds.Locale = CultureInfo.InvariantCulture ' See the FillDataSet method in the Loading Data Into a DataSet topic. FillDataSet(ds) Dim orders As DataTable = ds.Tables("SalesOrderHeader") ' Query the SalesOrderHeader table for orders placed ' after August 8, 2001. Dim query = _ From order In orders.AsEnumerable() _ Where order.Field(Of DateTime)("OrderDate") > New DateTime(2001, 8, 1) _ Select order ' Create a table from the query. Dim boundTable As DataTable = query.CopyToDataTable() ' Bind the table to a System.Windows.Forms.BindingSource object, ' which acts as a proxy for a System.Windows.Forms.DataGridView object. bindingSource.DataSource = boundTable