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.

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

Select<br />

The Select keyword (select <strong>in</strong> C#) is used to retrieve objects from the source you are query<strong>in</strong>g. In<br />

this example you see how to select an object of an exist<strong>in</strong>g type. Later <strong>in</strong> this chapter you see how to<br />

def<strong>in</strong>e new object shapes on the fly.<br />

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

Dim allReviews = From r In myEntities.Reviews<br />

Select r<br />

C#<br />

var allReviews = from r <strong>in</strong> myEntities.Reviews<br />

select r;<br />

The r variable <strong>in</strong> this example is referred to as a range variable that is only available with<strong>in</strong> the current<br />

query. You typically <strong>in</strong>troduce the range variable <strong>in</strong> the From clause, <strong>and</strong> then use it aga<strong>in</strong> <strong>in</strong> the<br />

Where <strong>and</strong> Select clauses to filter the data, <strong>and</strong> to <strong>in</strong>dicate the data you want to select. Although<br />

you can choose any name you like, you often see s<strong>in</strong>gle-letter variables like the r (for Review) or<br />

you see the s<strong>in</strong>gular form of the collection you are query<strong>in</strong>g (review <strong>in</strong>stead of r <strong>in</strong> the preced<strong>in</strong>g<br />

examples).<br />

From<br />

Although not considered a st<strong>and</strong>ard query operator — because it doesn’t operate on the data but<br />

rather po<strong>in</strong>ts to the data — the From clause (from <strong>in</strong> C#) is an important element <strong>in</strong> a LINQ query,<br />

because it def<strong>in</strong>es the collection or data source that the query must act upon. In the previous example,<br />

the From clause <strong>in</strong>dicates that the query must be executed aga<strong>in</strong>st the Reviews collection that is<br />

exposed by the myEntities object <strong>in</strong> EF.<br />

Order By<br />

With Order By (orderby <strong>in</strong> C#, without the space that <strong>VB</strong>.<strong>NET</strong> uses) you can sort the items <strong>in</strong><br />

the result collection. Order By is followed by an optional Ascend<strong>in</strong>g or Descend<strong>in</strong>g (ascend<strong>in</strong>g<br />

<strong>and</strong> descend<strong>in</strong>g <strong>in</strong> C#) keyword to specify sort order. You can specify multiple criteria by separat<strong>in</strong>g<br />

them with a comma. The follow<strong>in</strong>g query returns a list of genres, first sorted by SortOrder <strong>in</strong><br />

descend<strong>in</strong>g order, then sorted on their Name <strong>in</strong> ascend<strong>in</strong>g order (the default):<br />

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

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

Order By g.SortOrder Descend<strong>in</strong>g, g.Name<br />

Select g<br />

C#<br />

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

orderby g.SortOrder descend<strong>in</strong>g, g.Name<br />

select g;

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

Saved successfully!

Ooh no, something went wrong!