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