15.02.2015 Views

C# 4 and .NET 4

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

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

Updates ❘ 879<br />

objeCTquery query builder meThods<br />

OfType()<br />

OrderBy()<br />

Select() SelectValue()<br />

Skip() Top()<br />

Intersect() Union() UnionAll()<br />

desCriPTion<br />

This method specifies to return only those entities of a<br />

specific type. This is very helpful with TPH relations.<br />

This method is for defining the sort order of the entities.<br />

These methods return a projection of the results. Select()<br />

returns the result items in the form of a DbDataRecord;<br />

SelectValue() returns the values as scalars or complex<br />

types as defined by the generic parameter TResultType.<br />

These methods are useful for paging. Skip a number of items<br />

with the Skip() method <strong>and</strong> take a specified number as<br />

defined by the Top() method.<br />

These methods are used to combine two queries.<br />

Intersect() returns a query containing only the results<br />

that are available in both of the queries. Union() combines<br />

the queries <strong>and</strong> returns the complete result without<br />

duplicates. UnionAll() also includes duplicates.<br />

Let’s get into one example on how to use these Query Builder methods. With the next code snippet, the<br />

racers are filtered with the Where() method to return only racers from the USA; the OrderBy() method<br />

specifies descending sort order first based on the number of wins <strong>and</strong> next on the number of starts. Finally,<br />

only the first three racers are in the result using the Top() method:<br />

This is the result from this query:<br />

using (var data = new Formula1Entities())<br />

{<br />

string country = "USA";<br />

ObjectQuery racers = data.Racers.Where("it.Country = @Country",<br />

new ObjectParameter("Country", country))<br />

.OrderBy("it.Wins DESC, it.Starts DESC")<br />

.Top("3");<br />

foreach (var racer in racers)<br />

{<br />

Console.WriteLine("{0} {1}, wins: {2}, starts: {3}",<br />

racer.Firstname, racer.Lastname, racer.Wins, racer.Starts);<br />

}<br />

}<br />

Mario Andretti, wins: 12, starts: 128<br />

Dan Gurney, wins: 4, starts: 87<br />

Phil Hill, wins: 3, starts: 48<br />

code snippet Formula1Demo/Program.cs<br />

uPdaTes<br />

Reading, searching, <strong>and</strong> filtering data from the store are just one part of the work that usually needs to<br />

be done with data-intensive applications. Writing changed data back to the store is the other part you<br />

need to know.<br />

The sections that follow cover these topics:<br />

➤<br />

➤<br />

➤<br />

➤<br />

Object tracking<br />

Change information<br />

Attaching <strong>and</strong> detaching entities<br />

Storing entity changes<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!