Entity Framework basics-Article 5 (Model First), entityframework

Source: Internet
Author: User

Entity Framework basics-Article 5 (Model First), entityframework

First: load when used

static void Main(string[] args)        {            Query();        }        private static void Query()        {            DataModelContainer dbContext=new DataModelContainer();            IQueryable<UserInfo> userInfo = from u in dbContext.UserInfo                                            where u.UserName.Contains("s")                                            select u;            var resultstr = from u in userInfo                            where u.UserID>0                            select u;            foreach (var userinfoitem in resultstr)            {                Console.WriteLine(userinfoitem.UserID + "," + userinfoitem.UserName);            }             dbContext.SaveChanges();        }

Open SQL server profiler and run the following code:

Sometimes it is useless to use the data queried by Linq for caching, because SQL scripts are still executed.

Second, delayed loading:

Static void Main (string [] args) {// Add (); // Query (); QueryTwo (); Console. readKey () ;}//< summary> /// second type of delayed loading /// </summary> private static void QueryTwo () {DataModelContainer dbContext = new DataModelContainer (); IQueryable <UserInfo> userInfo = from u in dbContext. userInfo select u; foreach (var userInfoItem in userInfo) // You can first query {foreach (var orderInfoItem in userInfoItem. orderInfo) // filter data by navigation properties {Console. writeLine (userInfoItem. userName + "," + orderInfoItem. ID );}}}

Open SQL server profiler and run the following code:

Static void Main (string [] args) {// Add (); // Query (); QueryTwo (); Console. readKey () ;}//< summary> /// second type of delayed loading /// </summary> private static void QueryTwo () {DataModelContainer dbContext = new DataModelContainer (); IQueryable <UserInfo> userInfo = from u in dbContext. userInfo. include ("OrderInfo") // you can change it here, which means that when you query the data in the UserInfo table, select u is also queried together with the order; foreach (var userInfoItem in userInfo) // query {foreach (var orderInfoItem in userInfoItem. orderInfo) // filter data by navigation properties {Console. writeLine (userInfoItem. userName + "," + orderInfoItem. ID );}}}

Open SQL server profiler and run the following code:

Static void Main (string [] args) {// Add (); // Query (); // QueryTwo (); QueryPartColumn (); Console. readKey () ;}//< summary> /// query some columns /// </summary> private static void QueryPartColumn () {DataModelContainer dbContext = new DataModelContainer (); // first // var partData = from u in dbContext. userInfo // select new {u. userName, u. userID, OrderCounts = u. orderInfo. count}; // var partData = dbContext. userInfo. where <UserInfo> (u => u. userID> 0 ). select (u => new {u. userID, u. userName, u. orderInfo. count}); foreach (var item in partData) {Console. writeLine (item );}}

Paging:

Static void Main (string [] args) {// Add (); // Query (); // QueryTwo (); PageData (); Console. readKey () ;}/// <summary> // data paging // </summary> private static void PageData () {DataModelContainer dbContext = new DataModelContainer (); var DataPage = dbContext. userInfo. where <UserInfo> (u => u. userID> 0 ). orderBy <UserInfo, int> (u => u. userID) // The default value is ascending //. orderByDescending <UserInfo, int> (u => u. userID) // retrieve the first page. skip (5*(3-3 )). take (5); foreach (var item in DataPage) {Console. writeLine (item. userID + "," + item. userName );}}

A standard paging SQL statement is output.

 

OK. Write it here. If there is an error, please leave a message. Thank you!

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.