concept query - builder method in category .net

appears as: query-builder methods, The query-builder methods, query-builder methods
Entity Framework 4 in Action

This is an excerpt from Manning's book Entity Framework 4 in Action.

Because Entity SQL is string-based, query-builder methods, and Entity SQL in general, make runtime construction easy. The following listing shows the code required for runtime query construction using both LINQ to Entities and query-builder methods.

Listing 9.7. Chaining query-builder methods vs. chaining LINQ to Entities methods

Wow. Just one statement , as compared to seven . Isn’t that great? You should now understand why we said that Entity SQL in combination with query-builder methods is more useful than LINQ to Entities in such cases.

9.8. Using query-builder methods

Query-builder methods are a convenient way to organize Entity SQL queries and can often simplify dynamic query creation. These methods do exactly what their name suggests: they let you build an Entity SQL query using methods instead of having to write the entire Entity SQL on your own. Query-builder methods don’t cover all aspects of querying, but, in our experience, 90% of a project’s queries can be developed using them.

Query-builder methods aren’t extension methods like the LINQ to Entities methods. They’re included in the ObjectQuery<T> class and implement the fluent technique, which allows them to be chained, because they return an ObjectQuery<T> instance. The query-builder methods are listed in table 9.1.

Table 9.1. The query-builder methods

Method

Functionality

Distinct Specifies that returned data must be unique
Except Limits query results by excluding results based on the results of another object query
Include Eager-loads related associations
Intersect Limits query results by including only the results that exist in another object query
OfType<T> Retrieves only instances of objects of the specified type
OrderBy Defines the sorting properties
Select Defines the properties to retrieve
SelectValue<T> Defines the properties to retrieve, and returns them in the object specified
Skip Sorts the data by a key, and skips the first n occurrences
Top Returns only the first n occurrences
Union Merges the results of two queries, removing duplicates
UnionAll Merges the results of two queries
Where Defines filters
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest