The difference between IQueryable and IEnumberable

The difference between IQueryable and IEnumberable

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

IEnumerable interface

Exposes an enumerator that supports simple iteration on a collection of a specified type. In other words: an object that implements this interface can directly use foreach to traverse this object;

IQueryable interface

It inherits the IEnumerable interface, and because the .net version adds Linq and IQueryable, IEnumerable is no longer so monotonous, and becomes more powerful and rich.

In order to distinguish between the two interfaces, we explain through a practical example.

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

static void Main(string[] args)
        {
           //Create a database access gateway
            using (SchoolDBEntities schoolEntities = new SchoolDBEntities())
            {
               //The result of the query is put into the collection of the IQueryable interface
                IQueryable<T_Class> classesIQue = (from c in schoolEntities.T_Class
                                                   orderby c.ID
                                                     select c).Skip<T_Class>(3).Take<T_Class>(3);
               //Note that this AsEnumerable<T_Class>() converts it to IEnumerable type before paging query
                IEnumerable<T_Class> classesIEnu = (from c in schoolEntities.T_Class
                                                    orderby c.ID   
                                                    select c).AsEnumerable<T_Class>().Skip<T_Class>(3).Take<T_Class>(3);
               //Because the delayed loading mechanism is enabled, call it 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();
        }

Pay attention to the red code. Before querying the entity collection with linq, I will first convert it to the IEnumerable interface type to see how the final executed SQL is.

The first type: directly return IQueryable type query, as shown in the following figure:

The second: before using the paging query, first convert it to IEnumerable actually executed SQL as shown in the figure below:

summary

The difference between the IQueryable interface and the IEnumberable interface: The IEnumerable<T> generic class has already loaded the data in the local memory before calling its own extension methods such as SKip and Take, and IQueryable<T> is the expression of these methods like Skip and Take. After being translated into a T-SQL statement, the command is sent to the SQL server. It does not load all the data into the memory to perform conditional filtering.

Reference: https://cloud.tencent.com/developer/article/1503523 The difference between IQueryable and IEnumberable-Cloud + Community-Tencent Cloud