Databases Reference
In-Depth Information
NOTE
Each LINQ-enabled framework, such as Entity Framework or LINQ to SQL, provides its
own implementation of the IQueryProvider , which allows the same code to construct
LINQ queries independently of the underlying data access technology.
Query Logic in the Parent Filter Template
Instead of hard-coding type and property names as shown in the previous section, the
parent filter template builds the Join query dynamically, relying on the metadata
information describing the foreign key column and the child and parent tables it links
together. Still, the structure of the code is similar to the specific example just discussed
and should be easy for you to follow. Listing 12.9 shows the remaining implementation
of the Parent filter template. It includes the overridden GetQueryable method and the
helper methods it uses to build the required lambda expressions: BuildChildKeySelector ,
BuildParentKeySelector , and BuildResultSelector .
For a filter template, it is important to modify the query only if the user actually entered
search criteria in its control. As you are reading this code, remember that childQuery is an
IQueryable object created by the EntityDataSource control on the page (Product query in
the example). Notice how the code creates an empty query for the parent entity (a query
that returns all Suppliers) by calling the GetQuery method of the MetaTable object describ-
ing the parent entity. This empty parent query is passed to the GetQueryable method of
the nested filter template. Only if the query it returns is not the same as the original
empty query will the Parent filter template generate a Join query. If the nested filter
template returned the empty query without any filter criteria for the parent table, or in
other words, if the user did not enter any filter criteria for the parent table, adding a Join
to the child query is not necessary and would only slow it down.
NOTE
Calling the GetQuery method of the MetaTable object as opposed to using the corre-
sponding entity set property (such as Suppliers) of the ObjectContext directly is
important for consistency with the Dynamic Data architecture. The GetQuery method of
the MetaTable class is virtual and can be overridden to implement row-level security
as discussed in Chapter 14. It is also important to pass it the same ObjectContext
that was collected by the ContextCreated event handler discussed earlier, to ensure
that both queries in the join come from the same ObjectContext .
LISTING 12.9 Parent Filter Template (Query Logic)
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
 
Search WWH ::




Custom Search