DataTableExtensions.CopyToDataTable(T) Method (IEnumerable(T)) (System.Data)

Switch View :
ScriptFree
.NET Framework Class Library
DataTableExtensions.CopyToDataTable<T> Method (IEnumerable<T>)

Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Namespace:  System.Data
Assembly:  System.Data.DataSetExtensions (in System.Data.DataSetExtensions.dll)
Syntax

Visual Basic (Declaration)
<ExtensionAttribute> _
Public Shared Function CopyToDataTable(Of T As DataRow) ( _
	source As IEnumerable(Of T) _
) As DataTable
Visual Basic (Usage)
Dim source As IEnumerable(Of T)
Dim returnValue As DataTable

returnValue = source.CopyToDataTable()
C#
public static DataTable CopyToDataTable<T>(
	this IEnumerable<T> source
)
where T : DataRow

Visual C++
[ExtensionAttribute]
public:
generic<typename T>
where T : DataRow
static DataTable^ CopyToDataTable(
	IEnumerable<T>^ source
)
JScript
JScript does not support generic types or methods.

Type Parameters

T

The type of objects in the source sequence, typically DataRow.

Parameters

source
Type: System.Collections.Generic.IEnumerable<T>
The source IEnumerable<T> sequence.

Return Value

Type: System.Data.DataTable
A DataTable that contains the input sequence as the type of DataRow objects.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<T>. 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).
Exceptions

Exception Condition
ArgumentNullException

The source IEnumerable<T> sequence is null and a new table cannot be created.

InvalidOperationException

A DataRow in the source sequence has a state of Deleted.

The source sequence does not contain any DataRow objects.

A DataRow in the source sequence is null.

Remarks

The parameter T of the input parameter source can only be of type DataRow or a type derived from DataRow.

The input sequence can be any IEnumerable<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.

When a null reference or nullable type with a value of null is found in a source DataRow, this method replaces the value in the destination DataTable with a value of Value.

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).

Examples

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.

Visual Basic
' 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


C#
// Bind the System.Windows.Forms.DataGridView object
// to the System.Windows.Forms.BindingSource object.
dataGridView.DataSource = bindingSource;

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable orders = ds.Tables["SalesOrderHeader"];

// Query the SalesOrderHeader table for orders placed 
// after August 8, 2001.
IEnumerable<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
    select order;

// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();

// 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;


Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5
See Also

Reference

Community Content

Snixtor
Best Avoided
As noted above by DominicZ, this method throws an Exception if the collection is empty. Best bet? Do NOT use this method, instead use one of the overloads such as: CopyToDataTable(DataTable, LoadOption), which fortunately enough, does not throw an exception when the collection is empty.

DominicZ
How to get a row count, when you need to call .CopyToDataTable() and there are no results.
$0When retrieving the results from CopyToDataTable(), you may expect it to return 0 rows under the given LINQ filtering. However, an exception is thrown when no rows are returned.$0 $0 Since the EnumerableRowCollection does not have a .Count() method, the only way you can deal with it, is to check the first instance of the GetEnumerator() and see if its null. If the .Current is null, then there are no items in the Collection.$0 $0 Its a messy hack, but I haven't been able to find another way around it.$0 $0 E.g.$0 $0 $0// Create table and populate data
DataTable dt = new DataTable();
dt.Columns.Add("Column1",typeof(string));
dt.Rows.Add( new object[] { "sample1" });

// Filter by a non-existent field, so now dataRows is now an EnumerableRowCollection with no rows
var dataRows = from row in dt.AsEnumerable()
                          where row.Field<string>("Column1") eq "FAKE DATA"
                          select row;



// This next line throws an InvalidOperationException 
//// DataTable filteredResults = dataRows.CopyToDataTable();   


// Instead, create a new IEnumerator and iterate through it manually.
IEnumerator iEnum = dataRows.GetEnumerator();

bool noRows = false;

if(iEnum.Current == null)
{
     noRows=true;



if(noRows)
{
    ShowMessage("No data available");
}
else
{
     DataTable filteredResults = dataRows.CopyToDataTable();

     //   More DataTable processing here ....


     MyGridView1.DataSource = filteredResults;
     MyGridView1.DataBind();
}$0