Databases Reference
In-Depth Information
control relies on the built-in LINQ operators
Skip
and
Take
. The Entity Framework imple-
mentation of LINQ requires these operators to be preceeded by an
OrderBy
and, normally,
the
EntityDataSource
is responsible for generating it. However, it does not automatically
generate the
OrderBy
operator when the query has been modified by a
QueryExtender
,
which causes a runtime error. The
DataSourceSelecting
method in the filter template
supplies a default sort expression to the
EntityDataSource
control unless it was already
supplied by a
GridView
control in response to a user clicking one of the column headers.
Implementing Query Logic
As you might recall from a discussion in Chapter 5, query logic is implemented by over-
riding the
GetQueryable
method inherited from the
QueryableFilterUserControl
base
class. This method receives an
IQueryable
object as a parameter, modifies the query based
on the control or controls it displays, and returns the modified query. The built-in filter
templates that come with Dynamic Data implement query logic by creating lambda
expressions dynamically, using LINQ
Expression
objects. This task can be significantly
simplified with Dynamic LINQ, a sample project that comes with Visual Studio and allows
manipulating LINQ expressions as simple strings instead of objects. Unfortunately, as you
see shortly, Dynamic LINQ cannot help implementing complex logic the Parent filter
template requires, and you have to resort to the low-level LINQ
Expression
manipulation.
NOTE
This section walks you through implementation of a specific filter template. However, the
general approach of starting with the high-level LINQ query and progressively digging
down to deeper levels works well for developing any custom filter template in general.
Although you can simply get the Parent control from the topic's source code, reading this
section will help you learn how to build your own advanced filter templates that require
using low-level LINQ expressions.
LINQ Expression Mechanics
Before you jump into the implementation details of query logic for an advanced filter
template, like Parent, first figure out how you would implement it without the dynamic
filters. Here is a LINQ query you could have written to retrieve all products imported from
the UK:
using (var context = new NorthwindEntities())
{
IQueryable<Product> query =
from p in context.Products
where p.Supplier.Country == “UK”
select p;
// ...
}