ingeniously uses Linq to query datatable data
Although the Linq query is a powerful data operation tool, it does not seem to be directly applicable to DataTable. However, there is an elegant solution to solve this seemingly impossible problem.
Asnumeraable () Expansion method
The key to executing the linq query on Datatable is the Asenumeration () extension method. This method expands the DataTable class and returns an Ienumerable object. By calling this expansion method, you can use Linq syntax to access the ROWS collection of DataTable.
Example:
This query screens the datatable line that is equal to 1.
Other extension methods
<code class="language-csharp">var results = from myRow in myDataTable.AsEnumerable() where myRow.Field<int>("RowNo") == 1 select myRow;</code>
In addition to Asenumeration (), there are other useful DataTable operation extensions:
Field
Although the linq query does not directly support DataTable, the Asenumerat () expansion method provides a direct and effective method to perform these queries. This expansion method is together with other available extensions, so that you can make full use of Linq's powerful features to operate DataTable.
The above is the detailed content of How Can I Use LINQ to Query DataTables?. For more information, please follow other related articles on the PHP Chinese website!