Today to write the knowledge is really a bit around, for the first section of the content, in fact, the original in-memory data source, replaced by the data extracted from the database. From the way the code is used is the same, skip directly, to see the difference between IEnumerable and IQueryable.
Just as the enumerable type contains an extension method about ienumerable<t> to implement the LINQ standard query operators, the QUERYABL type contains an extension method for iqueryable<t>. There are two important differences between ienumerable<t> and iqueryable<t>.
First ienumerable<t> methods use delegates as arguments, for example, the Select method uses Func<tsource,tresult>. This is not a problem for in-memory operations, but for LINQ providers that execute queries elsewhere, we need a format that can perform a more detailed check-the expression tree. For example, the overload of the corresponding select in queryable gets a parameter of type expression<func<tsource,tresult>>. The compiler doesn't care about this at all-after the query compiles, it has a lambda expression that needs to be passed as a parameter to the method, and the lambda expression can be converted to a delegate instance or to an expression tree. , the appropriate overloads are selected based on the data source.
Select the query for both paths, whichever path you use depends on whether the data source implements the IQueryable interface or the IEnumerable interface.
When we pass a lambda expression as a parameter, the lambda expression can be converted to a delegate instance or to an expression tree instance, the left path is used to perform in-memory operations, and the right path is used to execute SQL in the database.
The 2nd significant difference between queryable and enumerable is that the extension method of enumerable completes the actual work associated with the corresponding query operator (at least the iterator that completes the work). For example, the code in Eumerable.where performs the relevant filtering operations and generates the appropriate elements in the result sequence. By comparison, the "implementation" of the query operator in queryable does very little.
- Introduction to iqueryable<t> and related interfaces
Iqueryable<t> is inherited from the ienumerable<t> and non-generic IQueryable, and IQueryable inherits from the non-generic IEnumerable, IQueryable has only three properties: Queryprovider, ElementType, and expression. The simplest way to understand IQueryable is to think of it as a query that, when executed, produces a sequence of results. From a LINQ perspective, the details of the query are saved in the expression tree because the result is returned through the expression property of IQueryable.
Please give us insights, learn one or two, treatise.
23.c#queryable extension Method (Chapter 12 12.1-12.2)