Databases Reference
In-Depth Information
with its dynamic equivalent.
IQueryable query = // …
query = query.Where(“City == @0”, “London”);
The dynamic Where method parses the string and creates a strongly-typed LINQ expression
tree that can be used by a LINQ-enabled data access framework, such as the ADO.NET
Entity Framework in this example. Following is the equivalent code that uses LINQ expres-
sion classes to accomplish the same result:
IQueryable query = // …
ParameterExpression parameter = Expression.Parameter(
query.ElementType, string.Empty);
Expression property = Expression.PropertyOrField(parameter, “City”);
Expression comparison = Expression.Equal(
property, Expression.Constant(“London”));
LambdaExpression lambda = Expression.Lambda(
comparison, new ParameterExpression[] { parameter });
MethodCallExpression where = Expression.Call(
typeof(Queryable), “Where”,
new Type[] { query.ElementType },
new Expression[] { query.Expression, Expression.Quote(lambda) });
query = query.Provider.CreateQuery(where);
Compared to raw expressions, dynamic LINQ extensions make implementing filter
templates much, much easier. However, parsing strings requires additional CPU resources
on the web server, and you might want to consider implementing filter templates with
LINQ expressions as an optimization. You might also need to use LINQ expressions if the
dynamic extensions do not support the LINQ functionality you need in your filter
template, such as joins. However, this topic is too vast and complex to cover here; it is
revisited in Chapter 12, “Building Custom Search Pages.”
Building Search Pages with Filter Templates
Now that you have implemented the filter template, Text.ascx , and saved it in the
DynamicData\Filters folder of this web project, you can rewrite the sample search page to
take advantage of its functionality. Compare Listing 5.5, which shows markup of the new
version of this page, with Listing 5.1, which shows the original version.
 
 
Search WWH ::




Custom Search