23.c#queryable extension Method (Chapter 12 12.1-12.2)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.