开发者

Filter rows in a table using ASP.NET Dynamic Data Entities

开发者 https://www.devze.com 2023-03-21 22:14 出处:网络
Currently, ASP.NET Dynamic Data Entities only supports filtering on boolean or Foreign Key relationships out-of-the-box

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, ba开发者_StackOverflow社区sed on a dropdown list of values to filter the rows by?


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";


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号