Книга: C# 2008 Programmer

Saving the Result of a Query to a DataTable

Saving the Result of a Query to a DataTable

The result of a LINQ query can be saved into a DataTable object by using the CopyToDataTable() method. The CopyToDataTable() method takes the result of a query and copies the data into a DataTable, which can then be used for data binding.

The following example shows a LINQ query using typed DataSet with the result copied to a DataTable object and then bound to a DataGridView control:

var query1 =
 from customer in ds.Customers
 where customer.Country == "USA"
 select customer;
DataTable USACustomers = query1.CopyToDataTable();
dataGridView1.DataSource = USACustomers;

Note that the CopyToDataTable() method only operates on an IEnumerable<T> source where the generic parameter T is of type DataRow. Hence, it does not work for queries that project anonymous types or queries that perform table joins.

Оглавление книги


Генерация: 1.333. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз