6. Explore features

Source: Internet
Author: User

Summary:

Learn how to use a LINQ statement such as cache, latency, restriction, and isolation.

Personal study notes are for personal review only. The learning Source is linked in the first article.

Content:

1. Delayed iqueryable execution

Iqueryable query = from C inctx. Customers select C;

Such a query syntax does not cause the statement to be executed immediately. It is just a description and corresponds to an SQL statement. The statement is executed only when necessary.

If you perform two foreach operations, the execution of the two SQL statements will be captured:

Iqueryable query = from C inctx. Customers select C;

Foreach (customer C inquery)

Response. Write (C. customerid );

Foreach (customerc in query)

Response. Write (C. contactname );

For such a requirement, we recommend that you first use tolist () and other methods to save the query results first, and then query the set:

Ienumerable <customer> MERs = (from C inctx. Customers select c). tolist ();

Foreach (customer C incustomers)

Response. Write (C. customerid );

Foreach (customer C incustomers)

Response. Write (C. contactname );

2,Dataloadoptions

Dataloadoptions Options = new dataloadoptions ();

Options. loadwith <product> (P => P. order_details );

Options. associatewith <product> (P => P. order_details.where (OD => OD. quantity> 80 ));

CTX. loadoptions = options;

VaR products = from P in CTX. Products select P;

Linqto SQL has restrictions on the use of dataloadoptions. It only supports one-to-many relationship.

Dataloadoptions Options = new dataloadoptions ();

Options. loadwith <customer> (C => C. Orders );

Options. loadwith <order> (O => O. order_details );

CTX. loadoptions = options;

Ienumerable <customer> MERs = CTX. Customers. tolist <customer> ();

For the many-to-one relationship, there is no restriction on the dataloadoptions:

Dataloadoptions Options = new dataloadoptions ();

Options. loadwith <product> (C => C. Category );

Options. loadwith <product> (C => C. order_details );

Options. loadwith <order_detail> (O => O. Order );

CTX. loadoptions = options;

Ienumerable <product> Products = CTX. Products. tolist <product> ();

3. Primary Key Cache

LINQ to SQL caches the queried objects. If a record is queried only based on the primary key

Read from the cache directly:

Customer C1 = CTX. Customers. Single (customer => customer. customerid = "anrecognition ");

C1.contactname = "Zhuye ";

Customer C2 = CTX. Customers. Single (customer => customer. customerid = "anrecognition ");

Response. Write (c2.contactname );

4. datacontext isolation

 

Sometimes we will pass the object to datacontext from the outside and require it to be updated, because different datacontext are relatively independent. Since the new datacontext does not obtain an object, we can only update the data by appending it.

First, add the primary key field of the customer table with the isversion ID:

[Column (storage = "_ customerid", dbtype = "nchar (5) not null", canbenull = false, isprimarykey = true, isversion = true)]

Run the following test code:

Customer c = new customer {customerid = "alfki", contactname = "Zhuye", companyName = "1111 "};

CTX. Customers. Attach (C, true );

CTX. submitchanges ();

 

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.