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.

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

anonymous type is a type whose name <strong>and</strong> members you don’t def<strong>in</strong>e up front as you do with other<br />

types. Instead, you construct the anonymous type by select<strong>in</strong>g data <strong>and</strong> then lett<strong>in</strong>g the compiler<br />

<strong>in</strong>fer the type for you. The anonymous type can only be accessed with<strong>in</strong> the method that declared it,<br />

<strong>and</strong> as such you cannot return an anonymous type from a method.<br />

If you don’t def<strong>in</strong>e the actual type <strong>and</strong> give it a name, how can you access the type <strong>and</strong> its properties?<br />

This is once aga<strong>in</strong> done with type <strong>in</strong>ference, where the compiler can see what data is assigned<br />

to a variable <strong>and</strong> then creates a new, anonymous type on the fly.<br />

Creat<strong>in</strong>g an anonymous type is easy; <strong>in</strong>stead of select<strong>in</strong>g the actual object us<strong>in</strong>g someth<strong>in</strong>g like<br />

Select review, you use the new keyword <strong>in</strong> C# <strong>and</strong> New With <strong>in</strong> Visual Basic, <strong>and</strong> then def<strong>in</strong>e the<br />

properties you want to select between a pair of curly braces:<br />

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

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

Where myReview.Authorized = True<br />

Select New With {myReview.Id, myReview.Title, myReview.Genre.Name}<br />

C#<br />

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

where review.Authorized == true<br />

select new { review.Id, review.Title, review.Genre.Name };<br />

Although the type is anonymous <strong>and</strong> cannot be accessed by name directly, the compiler is still able<br />

to <strong>in</strong>fer the type, giv<strong>in</strong>g you full IntelliSense for the new properties that were selected <strong>in</strong> the query.<br />

Figure 14-5 shows how you access the properties of the anonymous type <strong>in</strong> the authorizedReviews<br />

variable, us<strong>in</strong>g the var keyword <strong>in</strong> C#.<br />

FIGURE 14-5<br />

Note that the preced<strong>in</strong>g query accessed the actual Genre property of the Review. Besides its<br />

GenreId (def<strong>in</strong>ed as a column <strong>in</strong> the table Review <strong>in</strong> the database), the Review class also has a<br />

strongly typed Genre property, giv<strong>in</strong>g you direct access to the genre’s properties, like the Name, as<br />

the previous query demonstrates.<br />

Besides directly select<strong>in</strong>g exist<strong>in</strong>g properties — as shown <strong>in</strong> the query that selected the Id <strong>and</strong> Title<br />

of the Review <strong>and</strong> the Name of the Genre — you can also make up property values <strong>and</strong> give them

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

Saved successfully!

Ooh no, something went wrong!