EntityFramework ienumerable,iqueryable, Include

Source: Internet
Author: User

Using IQueryable

       Using (new centastaffentities ())            {                iqueryable<staff> queryablestaffs = db. Staff.asqueryable (). (P=>p.staffid). Skip (3). Take (3foreach (in queryablestaffs) {Console.WriteLine (item. Cnname); } }

Using IEnumerable

            using (varnew  centastaffentities ())            {                  IEnumerable<Staff> enumerablestaffs = db. Staff.asenumerable (). (p = p.staffid). Skip (3). Take (3);                 foreach (var in enumerablestaffs)                {                    Console.WriteLine (item. cnname);                }            }

the difference between the IQueryable interface and the Ienumberable interface: ienumerable<t> generic classes before calling their own extension methods such as Skip and take, the data is already loaded in local memory, and Iqueryable<t> is the skip These method expressions are translated into T-SQL statements before sending commands to the SQL Server, which does not load all the data into memory for conditional filtering.

Reference: http://www.cnblogs.com/ambon/articles/4766924.html

Include (), the two tables must contain foreign key relationships, just specify the class property name corresponding to the key name, and you do not need to specify the result field (that is, all mappings). By default, when a table is searched, the foreign key table is not queried, and the database query is not read until it is actually used, and if Include () is used, the specified foreign key table information is read when the table is read.

using (varnew  testentities ())            {                IEnumerable<Student> enumerablestaffs = db. Student;                 foreach (var in enumerablestaffs)                {                    // gets                     Console.WriteLine (item) every time. class.name);                }            }

Include

            using (varnew  testentities ())            {                // preload associated table  must have primary foreign key relationship                ienumerable<student> enumerablestaffs = db. Student.include ("Class");                 // query only once                foreach (var in enumerablestaffs)                {                    Console.WriteLine (item. class.name);                }            }

Left Join

EntityFramework ienumerable,iqueryable, Include

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.