23. C # Queryable Extension Method (Chapter 12 12.1-12.2 ),

Source: Internet
Author: User

23. C # Queryable Extension Method (Chapter 12 12.1-12.2 ),

The knowledge to be written today is really a bit difficult. For the content in Section 1, it is actually to replace the data source originally in the memory with the data extracted from the database. The code usage is the same. skip this step to see the differences between IEnumerable and IQueryable.

Just as the Enumerable type contains an extension method for IEnumerable <T> to implement the LINQ standard query operator, the Queryabl type contains an extension method for IQueryable <T>. There are two important differences between IEnumerable <T> and IQueryable <T>.

First, the IEnumerable <T> method uses delegation as the parameter. For example, the Select method uses Func <TSource, TResult>. This is no problem for operations in the memory, but for the LINQ provider that executes the query elsewhere, we need to be able to execute a more detailed check format-Expression Tree. For example, the corresponding Select overload in Queryable gets the parameter of Type Expression <Func <TSource, TResult>. The compiler does not care about this at all-after the query is compiled, it has a Lambda expression that needs to be passed to the method as a parameter, and the Lambda expression can be converted to a delegate instance, it can also be converted into an Expression Tree. The corresponding overload is selected based on the data source.

Select two query paths. The specific path used depends on whether the data source implements the IQueryable interface or the IEnumerable interface.

When we pass in a Lambda expression as a parameter, the Lambda expression can be converted to a delegated instance or an Expression Tree instance. The path on the left is used to perform operations in the memory, the right path is used to execute SQL statements in the database.

There are 2nd major differences between Queryable and Enumerable. The extension method of Enumerable will complete the actual work related to the corresponding query operator (at least build the iterator that completes the work ). For example, the Code in Eumerable. Where Performs related filtering operations and generates appropriate elements in the result sequence. By comparison, the "Implementation" of the query operator in Queryable does very little.

  • IQueryable <T> and related interfaces

IQueryable <T> is inherited from IEnumerable <T> and non-generic IQueryable, while IQueryable inherits from non-generic IEnumerable. IQueryable has only three attributes: QueryProvider, ElementType, and Expression. The simplest way to understand IQueryable is to regard it as a query and generate a sequence of results during execution. From the perspective of LINQ, because the results are returned through the Expression attribute of IQueryable, the detailed query information is saved in the Expression Tree.

Please give us some insights and learn one or two.

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.