03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Mapp<strong>in</strong>g Your Data Model to an Object Model x 505<br />

the myEntities variable holds a (scarce) connection to the SQL Server database, it’s a good idea to<br />

wrap the code that uses it <strong>in</strong> a Us<strong>in</strong>g block, so the object is destroyed at the end of the block <strong>and</strong> the<br />

connection is released. This myEntities object then exposes your data (such as reviews <strong>and</strong> genres)<br />

that you can use <strong>in</strong> a query:<br />

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

Us<strong>in</strong>g myEntities As New PlanetWroxEntities()<br />

Dim authorizedReviews = From review In myEntities.Reviews<br />

Where review.Authorized = True<br />

Order By review.CreateDateTime Descend<strong>in</strong>g<br />

Select review<br />

...<br />

End Us<strong>in</strong>g<br />

C#<br />

us<strong>in</strong>g (PlanetWroxEntities myEntities = new PlanetWroxEntities())<br />

{<br />

var authorizedReviews = from review <strong>in</strong> myEntities.Reviews<br />

where review.Authorized == true<br />

orderby review.CreateDateTime descend<strong>in</strong>g<br />

select review;<br />

...<br />

}<br />

Note that this query looks similar to the SQL that you learned <strong>in</strong> the previous chapters. Under the<br />

hood, the run time converts this LINQ query <strong>in</strong>to its SQL counterpart <strong>and</strong> executes it aga<strong>in</strong>st the<br />

underly<strong>in</strong>g database. With<strong>in</strong> this query, the variable review <strong>in</strong> the From clause is used to refer to the<br />

review <strong>in</strong> the other parts of the query (Where, Order By, <strong>and</strong> Select), enabl<strong>in</strong>g you to specify the<br />

select, filter, <strong>and</strong> order<strong>in</strong>g criteria.<br />

What’s important to realize is that EF uses a mechanism called lazy load<strong>in</strong>g, which means sub objects<br />

are not loaded until you explicitly tell them to. What this means is that <strong>in</strong> the previous example the<br />

Genre properties of the Review objects you’ve queried are null <strong>in</strong> C# <strong>and</strong> Noth<strong>in</strong>g <strong>in</strong> <strong>VB</strong>.<strong>NET</strong> <strong>and</strong><br />

don’t conta<strong>in</strong> any data. As soon as your code tries to access them, they are loaded by execut<strong>in</strong>g another<br />

query to the database. This can greatly improve performance if you don’t need these sub objects.<br />

However, if you’re sure you need them <strong>in</strong> your code beforeh<strong>and</strong>, execut<strong>in</strong>g a separate SQL statement for<br />

each item results <strong>in</strong> a lot of overhead. In that case, you can preload the objects with the <strong>in</strong>itial query. To<br />

express that you want to <strong>in</strong>clude these objects as well, you use the Include method for the types you<br />

want to query:<br />

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

Dim authorizedReviews = From review In myEntities.Reviews.Include("Genre")<br />

Where review.Authorized = True<br />

...<br />

C#<br />

var authorizedReviews = from review <strong>in</strong> myEntities.Reviews.Include("Genre")<br />

where review.Authorized == true<br />

...<br />

With this addition to the query, the Review objects now have their Genre property correctly filled with<br />

data. Though this may seem a little counter<strong>in</strong>tuitive <strong>and</strong> counterproductive at first, it’s actually quite

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

Saved successfully!

Ooh no, something went wrong!