IQueryable interface compared to IEnumerable interface

Source: Internet
Author: User
Tags what sql



Today when writing code to IQueryable inside to see its method, found that it is to implement the IEnumerable interface, then I ' M confused!




to distinguish between them, we should write a code to see it ~




we take out the previous practice of EF additions and deletions to change the code to change:


           

  #region querying a single entity using (var dbContext = new Secondchargeentities ())//Create a portal to access the database first {// var mystudent = dbContext.T_StuInfo.Find ("01");//Use the primary key to find the entity//console.writeline of the context trace (mystudent.  Stuname); Print out student name: Jay Brother//The results of the query are placed in the IQueryable interface's collection iqueryable<t_stuinfo> Stus = (from Stu in DbC Ontext. T_stuinfo Stu. Stucardnum select Stu). Skip<t_stuinfo> (3).                Take<t_stuinfo> (3); Before we skip, we'll turn the results of the query into IEnumerable ienumerable<t_stuinfo> mystus = (from Stu in Dbcontex asenumerable T.t_stuinfo Stu. Stucardnum select Stu). AsEnumerable < t_stuinfo> (). Skip<t_stuinfo> (3).                Take<t_stuinfo> (3);                Because the lazy loading mechanism is enabled, the following call will actually read the db int i = 0;                foreach (var s in stus) {i++;                } Console.WriteLine (i);                int j = 0; foreach (Var sTu in mystus) {j + +;            } Console.WriteLine (j);            } Console.WriteLine ("Baby, read it, see what SQL Profiler says ~ ~");            Console.readkey (); #endregion

After the run is complete:




Let's take a look at the bottom-level of what the underlying SQL statements of these two queries look like:



The first is the IQueryable interface:



As you can see, the real query is the SELECT Top 3. And then a lump of something else, estimated directly to find out the three data I want;



Then look at what SQL is generated when the IEnumerable interface query:





OMG, this query is actually full-check, and then loaded into small memory and then back from the memory of the 3 results I want.


From these two lines of code, we can see:


ienumerable<t> Generic classes 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 a T-SQL statement and then send a command to the SQL Server, it is not to load all the data into memory before the conditional filtering.





Summary:


now, let's summarize the pros and cons of these two interfaces in terms of functionality:














IQueryable interface compared to IEnumerable interface

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.