Custom predicate building when filtering

2 posts, 0 answers
  1. Vitala
    Vitala avatar
    1 posts
    Member since:
    Dec 2013

    Posted 26 Dec 2013 Link to this post

    IQueryable extension method ToDataSourceResult currently creates query like
     when using Contains operator.

     Unfortunately I'm using no-sql database RavenDb and its linq provider doesn't support contains method. Instead it offers extension for IQueryable called Search.
    So when doing full-text search I should write myQuery.Search(x=>x.SearchField, searchString) instead Where clause. That's why I'm curious is there any opportunity to override default predicate builder?
    Right now I'm doing it so (it's very basic implementation):
            public static IQueryable<TEntity> ApplyFiltering<TEntity>(this IQueryable<TEntity> queryable, IList<IFilterDescriptor> filters) where TEntity:class
                foreach (var filter in filters.Cast<FilterDescriptor>()){
                    var pe = Expression.Parameter(typeof(TEntity), "x");
                    var left = Expression.Property(pe, typeof(TEntity).GetProperty(filter.Member));
                    queryable = queryable.Search(Expression.Lambda<Func<TEntity, object>>(left, new ParameterExpression[] { pe }), filter.Value.ToString());
                return queryable;
    //And controller code:
            public ActionResult Data_Read([DataSourceRequest] DataSourceRequest request)
                    var query = repository.GetQueryable();
                    query = query.ApplyFilters(request.Filters);
                    return Json(query.ToDataSourceResult(request));
    Is it correct workaround? Or there is more obvious solution that I couldn't find in docs?
  2. Atanas Korchev
    Atanas Korchev avatar
    8462 posts

    Posted 27 Dec 2013 Link to this post


    Yes, this is the suggested workaround. Just make sure to clear the Filters of the DataSourceRequest before calling ToDataSourceResult in order to avoid applying them twice. Simply setting the Filters property to null after calling ApplyFilters should do the trick.

    Atanas Korchev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top