Home >Backend Development >C++ >How Can I Perform LINQ Queries on DataTables?

How Can I Perform LINQ Queries on DataTables?

Linda Hamilton
Linda HamiltonOriginal
2025-01-30 03:16:08868browse

How Can I Perform LINQ Queries on DataTables?

Use Linq to query datata: complete guide

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

expansion method provided by 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

🎜> fields equal to 1:

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

expansion method.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn