Filter associated objects: foreign key values vs navigation attributes

Source: Internet
Author: User

As shown in the preceding figure, the categoryid in the product can be empty. If you want a product without a category, you can use either of the following methods.

1. Foreign key value: the foreign key value is null.

2. Navigation property: the category in navigation property is empty.

 

Which method is better? What are the differences between the two methods?

Code

// Foreign key value
VaR query1 = From P In Context. Products
Where P. categoryid ! =   Null
Select P;

// Navigation Property
VaR query2 = From P In Context. Products
Where P. Category ! =   Null
Select P;

 

The two LINQ query statements implement the same results, but the implementation process is different:

Code Response. Write ( " Foreign key value: "   +   " <Br/> "   + (Objectquery < Product > ) Query1). totracestring () +   " <Br/> " );
Response. Write ( " Navigation property: "   +   " <Br/> " + (Objectquery < Product > ) Query2). totracestring ());

 

The result is as follows: the navigation property method is useless.CodeThe code generated by the foreign key value method is more concise and similar to the code used by SQL.

Therefore, it is better to use the foreign key in this case.

 

 

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.