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!