03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

506 x CHAPTER 14 LINQ AND THE ADO.<strong>NET</strong> ENTITY FRAMEWORK<br />

a nice feature. If you don’t need the extra Genre property <strong>in</strong> a specific page, you don’t take the performance<br />

hit of select<strong>in</strong>g <strong>and</strong> return<strong>in</strong>g these objects. If you do need them, all you need to add is a s<strong>in</strong>gle<br />

call to Include.<br />

Besides the Reviews collection the model also conta<strong>in</strong>s a Genres collection. When you want to select<br />

all the genres <strong>in</strong> the database, you can use this query:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Dim allGenres = From genre In myEntities.Genres<br />

Order By genre.Name<br />

Select genre<br />

C#<br />

var allGenres = from genre <strong>in</strong> myEntities.Genres<br />

orderby genre.Name<br />

select genre;<br />

In addition to these two separate objects <strong>and</strong> their collections, both objects have properties that refer<br />

to each other’s type. For example, a Review <strong>in</strong>stance has a Genre property that provides additional<br />

<strong>in</strong>formation about the genre to which the review was assigned. A Genre <strong>in</strong>stance <strong>in</strong> turn has a Reviews<br />

collection property, giv<strong>in</strong>g you access to all reviews posted <strong>in</strong> that genre. You see later how to make use<br />

of these properties.<br />

From the keywords used <strong>in</strong> the first query <strong>in</strong> this Try It Out, it’s probably easy to see what the query<br />

does: It gets a list of all the reviews <strong>in</strong> the system that have been authorized <strong>and</strong> orders them <strong>in</strong> descend<strong>in</strong>g<br />

order on their creation date. The result of the query is then assigned to the authorizedReviews<br />

variable. Notice that <strong>in</strong> both languages you can spread out the query over multiple l<strong>in</strong>es to improve<br />

readability. This is not required, but you’re encouraged to do it anyway because it makes your queries a<br />

lot easier to underst<strong>and</strong> <strong>and</strong> ma<strong>in</strong>ta<strong>in</strong>.<br />

You may notice some strange syntax <strong>in</strong> the query. The <strong>VB</strong>.<strong>NET</strong> example doesn’t use an As clause to<br />

def<strong>in</strong>e the type of the variable. Similarly, the C# snippet uses the var keyword, also without a type<br />

name. Although you may not conclude it from these code snippets, <strong>in</strong> both languages the variable<br />

authorizedReviews is still strongly typed <strong>and</strong> not just a variable with an undef<strong>in</strong>ed type.<br />

NOTE Strongly typed refers to the fact that the variable’s type is explicitly<br />

def<strong>in</strong>ed when it’s declared. Once you’ve defi ned the type for a variable (us<strong>in</strong>g<br />

Dim <strong>in</strong> <strong>VB</strong> or the type’s name <strong>in</strong> C#) you cannot change it anymore at run time.<br />

Strongly typed languages — such as C# <strong>and</strong> <strong>VB</strong>.<strong>NET</strong> — br<strong>in</strong>g many advantages,<br />

<strong>in</strong>clud<strong>in</strong>g the ability to check the types be<strong>in</strong>g used at compile time, someth<strong>in</strong>g<br />

that a weakly typed programm<strong>in</strong>g language cannot do.<br />

Because the code didn’t state the type for authorizedReviews (the example used Dim or var <strong>in</strong>stead),<br />

.<strong>NET</strong> needs a different solution to determ<strong>in</strong>e the type. This is done by a concept called type <strong>in</strong>ference,<br />

where the compiler is able to <strong>in</strong>fer the type for a variable by look<strong>in</strong>g at the right side of the assignment.<br />

In this case, the compiler sees that a list of Review objects will be returned from the query,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!