Language preference:

Phase 1 / EntityFilters

Filtering is an essential element of developing queries for your Select methods. A filter establishes a limit. Data that does not match that limit is excluded.

EntityFilters are a new type of class for developing queries with these characteristics:

  • Can generate the query either using LINQ or in the syntax compatible with a SQL Where clause.
  • Can be passed into the Select method, allowing the user to introduce filters that are not built into the Select method.
  • Are not required for developing your LINQ queries. They work together with your own LINQ code. However, they provide richly defined tools so you don't have to recreate common queries.
  • They provide the caller with user friendly descriptions of their settings, for use in the user interface's filter summary.
  • They are the underlying technology to BLD's user interface controls for filtering.

Here is a summary the EntityFilter classes. This first group works both within your Data Access Object's Select method and the user interface.

Class Description
CompareToValueEntityFilter Compares the data field to a value with a comparison operator.
TextSearchEntityFilter Looks through a textual data field for a string. Its Operator property determines how to match: Exact, Contains, StartsWith, EndsWith, AnyWords, and AllWords.
MultiFieldTextSearchEntityFilter Looks through a list of textual data fields for a string. Its Operator property determines how to match: Exact, Contains, StartsWith, EndsWith, AnyWords, and AllWords. It can automatically select data field names that have the FilterAttribute with InMultfieldSearch=true.
RangeEntityFilter Determines that the data field is inside a range.
SmartDateRangeEntityFilter For Date DataFields, it creates a range based on relative date rules, such as “Last week” and “Year to date”.
IsNullEntityFilter, IsEmptyTextEntityFilter, and IsNullOrEmptyTextEntityFilter These determine if the DataField is null or when textual, has no text. Use the NotOperator to test for not null or it has text.
PrimaryKeysEntityFilter Find a single record based on its primary key DataField values.
ForeignKeyEntityFilter Find a list of records that match the foreign key DataField values of the foreign key relationship identified in the DataField property.
OrEntityFilters and AndEntityFilters Performs either OR or AND Boolean logic on a list of EntityFilter objects.
CustomEntityFilter Uses event handler hooks to handle most of the work. Allows you to add code into your Data Access Object class specific to your select method.

Here are the user interface oriented EntityFilters and are passed into the Select method through its SelectArgs parameter. They cannot be created within Select methods because these methods are in the Business Logic Layer. (The Description column mentions various user interface elements that you haven't been introduced to yet.)

Class Description
FilterTemplatesEntityFilter Gets data from the filter user interface controls (BLDFilterField, BLDPatternForFilterFields, and BLDWidgetsView).
DataBoundControlEntityFilter Gets data from one Databound control (like ListView or FormView) to be used by a query for a second Databound control. Effectively creates a master/slave relationship between two queries.
UIDataFieldsFinderEntityFilter It looks for data in several locations: query string parameters, Url Routing data, and values established by the EntityDAODataSource.PrepareDefaultValues event.
QueryStringEntityFilter Looks at data from query string parameters
RouteDataEntityFilter Looks at data from ASP.NET Url Routing data.

The EntityFilter framework is intended for expansion. BLD provides detailed documentation to assist you as you add EntityFilter classes.

Since you have already seen a RangeEntityFilter in the context of a Select method, let's look at how you would implement a range that works with LINQ using various techniques.

Using RangeEntityFilter

public IEnumerable<Product> SelectPriceRange(
  decimal startPrice, decimal endPrice, SelectArgs selectArgs)
{
  if (endPrice == 0)
     endPrice = 999999999.99M;
  IQueryable query = GetTableAsQueryable();

  selectArgs.EntityFilters.Add(new RangeEntityFilter("UnitPrice", startPrice, endPrice));

  return ExecuteSelectCommand(query, selectArgs);
}

Using LINQ to SQL

public IEnumerable<Product> SelectPriceRange(
  decimal startPrice, decimal endPrice, SelectArgs selectArgs)
{
  if (endPrice == 0)
     endPrice = 999999999.99M;
  IQueryable query = GetTableAsQueryable();

  query = query.Where(p => p.UnitPrice >= startPrice && p.UnitPrice <= endPrice);

  return ExecuteSelectCommand(query, selectArgs);
}

Using LINQ Dynamic Query Library

The LINQ Dynamic Query Library is a free tool that lets you write queries in a hybrid of SQL and .net classes.

using System.Linq.Dynamic;

public IEnumerable<Product> SelectPriceRange(
  decimal startPrice, decimal endPrice, SelectArgs selectArgs)
{
  if (endPrice == 0)
     endPrice = 999999999.99M;
  IQueryable query = GetTableAsQueryable();

  query = query.Where("UnitPrice >= @0 && UnitPrice <= @1", startPrice, endPrice);

  return ExecuteSelectCommand(query, selectArgs);
}

In the next topic, you will learn about the Descriptor classes, which describe your database.