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.

878 ❘ ChaPTer 31 AdO.net entity frAmewOrk<br />

Now it would be interesting to filter the racers based on a condition. This can be done by using the Where()<br />

method of the ObjectQuery class. Where() is one of the Query Builder methods that create Entity<br />

SQL. This method requires a predicate as a string, <strong>and</strong> optional parameters of type ObjectParameter.<br />

The predicate shown here specifies that only the racers from Brazil are returned. The it specifies the item<br />

of the result <strong>and</strong> Country is the column Country. The first parameter of the ObjectParameter constructor<br />

references the @Country parameter of the predicate, but doesn’t list the @ sign:<br />

string country = "Brazil";<br />

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

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

The magic behind it can be seen immediately by accessing the Comm<strong>and</strong>Text property of the query. With<br />

Entity SQL, SELECT VALUE it declares it to access the columns:<br />

SELECT VALUE it<br />

FROM (<br />

[Formula1Entities].[Racers]<br />

) AS it<br />

WHERE<br />

it.Country = @Country<br />

The method ToTraceString() shows the generated SQL statement:<br />

SELECT<br />

[Extent1].[Id] AS [Id],<br />

[Extent1].[Firstname] AS [Firstname],<br />

[Extent1].[Lastname] AS [Lastname],<br />

[Extent1].[Country] AS [Country],<br />

[Extent1].[Starts] AS [Starts],<br />

[Extent1].[Wins] AS [Wins]<br />

FROM [dbo].[Racers] AS [Extent1]<br />

WHERE [Extent1].[Country] = @Country<br />

Of course, you can also specify the complete Entity SQL:<br />

string country = "Brazil";<br />

ObjectQuery racers = data.CreateQuery(<br />

"SELECT VALUE it FROM ([Formula1Entities].[Racers]) AS " +<br />

"it WHERE it.Country = @Country",<br />

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

The class ObjectQuery offers several Query Builder methods as explained in the following table. Many<br />

of these methods are very similar to the LINQ extension methods that you learned about in Chapter 11,<br />

“Language Integrated Query.” An important difference with the methods here is that instead of parameters<br />

of type delegate or Expression, the parameter type with ObjectQuery is usually of type string.<br />

objeCTquery query builder meThods<br />

Where()<br />

Distinct()<br />

Except()<br />

GroupBy()<br />

Include()<br />

desCriPTion<br />

This method allows you to filter the results based on a<br />

condition.<br />

This method creates a query with unique results.<br />

This method returns the result without the items that meet<br />

the condition with the except filter.<br />

This method creates a new query to group entities based on<br />

a specified criteria.<br />

With relations, you saw earlier that related items are delay<br />

loaded. It is required to invoke the Load() method of the<br />

EntityCollection class to get related entities into the<br />

object context. Instead of using the Load() method, you can<br />

specify a query with the Include() method to eager fetchrelated<br />

entities.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!