Home >Backend Development >C++ >How Can I Perform LINQ Queries on DataTables?
Although it is generally believed that the DATATATABLE cannot be used directly, this can actually be achieved through some techniques. Datatable does not implement the
interface by default, so it cannot directly query its ROWS collection.
IEnumerable<T>
The solution is to use the
assembly. By calling this method on DataTable, you can get a System.Data.DataSetExtensions
object, and then you can use it for linq query. AsEnumerable()
IEnumerable<DataRow>
For example, the following inquiries will return all the lines that
myDataTable
RowNo
You can also use Lambda expression to create Linq query:
<code class="language-csharp">var results = from myRow in myDataTable.AsEnumerable() where myRow.Field<int>("RowNo") == 1 select myRow;</code>
Please note that you need to add a reference to the
assembly in the project to access these extensions.<code class="language-csharp">var result = myDataTable.AsEnumerable() .Where(myRow => myRow.Field<int>("RowNo") == 1);</code>
If you need to convert the query results System.Data.DataSetExtensions
back to DataTable, you can use the
The above is the detailed content of How Can I Perform LINQ Queries on DataTables?. For more information, please follow other related articles on the PHP Chinese website!