The difference between IQueryable and ienumberable

Source: Internet
Author: User

IEnumerable interface

Exposes an enumerator that supports simple iterations on a collection of the specified type. That is, the object of this interface is implemented, and the object can be traversed directly using foreach;

IQueryable interface

It inherits the IEnumerable interface, and since the. NET version joins LINQ and IQueryable, the IEnumerable is no longer so monotonous, and becomes more powerful and rich.

To differentiate two interfaces, let's explain it in a practical example.

According to the example of the previous essay, write the following code:

static void Main (string[] args) {//CREATE database Access gateway using (schooldbentities schoolentities = new Sc Hooldbentities ()) {//The result of the query is placed in the IQueryable interface's collection iqueryable<t_class> Classesiq                                                     UE = (from C in Schoolentities.t_class c.id Select C). Skip<t_class> (3).                Take<t_class> (3); Note that this asenumerable<t_class> () is converted to IEnumerable type ienumerable<t_class> Classesienu = (FR) Before paging the query                                                       Om C in Schoolentities.t_class c.id Select C). AsEnumerable<T_Class>().Skip<t_class> (3).                Take<t_class> (3);                Because the lazy loading mechanism is enabled, it is called below to actually read the database int i = 0;                foreach (var c in classesique) {i++;                } Console.WriteLine (i);                foreach (var c in Classesienu) {i++;            } Console.WriteLine (i);            } Console.WriteLine ("OK");        Console.readkey (); }


Note The Red Code section, before querying the Entity collection with LINQ, I'll first convert it to the IEnumerable interface type to see what the final SQL will look like.

The first: Directly returns a query of type IQueryable, as shown in:

The second: Before paging through the query, convert it to IEnumerable actually executed SQL as shown:

Summarize

IQueryable interface differs from ienumberable interface: ienumerable<t> generic class The data is already loaded in local memory before calling its own extension methods such as Skip and take, and iqueryable<t > is to skip, take these method expressions into T-SQL statements and then send a command to the server, it is not to load all the data into memory for conditional filtering.

Transfer from http://www.cnblogs.com/fly_dragon/archive/2011/02/21/1959933.html

The difference between IQueryable and ienumberable

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.