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.