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