4

Currently, ASP.NET Dynamic Data Entities only supports filtering on boolean or Foreign Key relationships out-of-the-box

How would I implement a custom filter, based on a dropdown list of values to filter the rows by?

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
manning18
  • 957
  • 3
  • 8
  • 18

2 Answers2

0

I have done something similar using just adding where parameters to the GridView's datasource. Suppose your user selects Color Code 9 from a drop down.On Page_Init, set the parameter.

Parameter p = new Parameter { Name = "ColorSelection", Type = TypeCode.Int32, DefaultValue = "9" };
GridDataSource.WhereParameters.Add(p);
GridDataSource.Where = "ColorId == @ColorSelection";
Ash Machine
  • 9,601
  • 11
  • 45
  • 52
  • I did consider taking such an approach, but the problem with doing it that way is that we need to know in advance, at compile time, the underlying details of the table, eg type, column name. In order to solve the problem in a generic way, I did some research and discovered that one must use expression trees to dynamically compose LINQ predicates on the underlying IQueryable attached to Column.ParentTable.GetQuery() – manning18 Jul 26 '11 at 22:13
0

I figured out a way to do this, so that it fits within the intended templating system that ASP.NET Dynamic Data uses

Basically the problem with this is that we do not know at compile time what the underlying column that might be filtered on is, let alone what type or column name it is - the first clue that led me to the path to implementing a solution was that the Filter controls all have a method GetQueryable

Unfortunately, this method works on an instance of IQueryable rather than IQueryable<T>, thus preventing us from being able to simply use standard LINQ operators. What we need is a way to dynamically create a predicate (or chain of predicates even - in this case however, all I wanted to do was to get the distinct entries and sort them), which we can then apply to the IQueryable

Unfortunately, there is no out of the box implementation of Dynamic LINQ, thus we must use expression trees - the second hint of how to solve this problem is the fact the IQueryable object exposes a .Expressions collection

What we need to do is create the expression tree to represent the predicate (for selecting distinct in this case) then attach it to the IQueryable and return it. This was certainly not an obvious, nor straight-forward task and I certainly did not find expression trees easy to understand but therein was how I solved the problem.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
manning18
  • 957
  • 3
  • 8
  • 18