Filter rows in a table using ASP.NET dynamic data objects

Currently, ASP.NET Dynamic Data Entities only supports filtering by logical or foreign keys from ready-made

How to implement a custom filter based on a drop-down list of values ​​to filter rows on?

+4
source share
2 answers

I figured out a way to do this so that it matches the intended template system that uses ASP.NET dynamic data

Basically, the problem is that at compile time we don’t know what the base column can be filtered out, not to mention what type or name of the column it is - the first key that led me to the implementation path the solution was that all Filter controls have a GetQueryable method

Unfortunately, this method only works with an instance of IQueryable , not IQueryable<T> , which does not allow us to simply use the standard LINQ operators. We need a way to dynamically create a predicate (or a chain of predicates - in this case, however, all I wanted to do was get individual records and sort them), which we can then apply to IQueryable

Unfortunately, there is no built-in implementation in Dynamic LINQ, so we have to use expression trees. The second hint on how to solve this problem is that the IQueryable object provides a collection of .Expressions

We need to create an expression tree to represent the predicate (to select in this case, the choice), then attach it to IQueryable and return it. This, of course, was not an obvious or direct task, and of course I did not find that expression trees are easy to understand, but this is how I solved the problem.

0
source

I did something similar by simply adding parameters to the GridView data source. Suppose your user selects Color Code 9 from the drop-down list. On the Page_Init page, set the parameter.

 Parameter p = new Parameter { Name = "ColorSelection", Type = TypeCode.Int32, DefaultValue = "9" }; GridDataSource.WhereParameters.Add(p); GridDataSource.Where = "ColorId == @ColorSelection"; 
0
source

All Articles