This is a migrated thread and some comments may be shown as answers.

Custom predicate building when filtering

1 Answer 461 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vitala
Top achievements
Rank 1
Vitala asked on 26 Dec 2013, 05:36 PM
Hello. 
IQueryable extension method ToDataSourceResult currently creates query like
myQuery.Where(x=>x.SearchField.Contains(searchString))
 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());
            }
            filters.Clear();
 
            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?

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 27 Dec 2013, 09:57 AM
Hello,

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.

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Vitala
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or