Custom Predicate Building with Kendo UI MVC Grid Filtering

I am using the Kendo interface for ASP.NET MVC with server wrappers.

The IQueryable extension method ToDataSourceResult is currently creating a query of type

 myQuery.Where(x=>x.SearchField.Contains(searchString))

when using the Contains operator to filter.

Unfortunately, I am using the no-sql RavenDb database, and its linq provider does not support the contains method. Instead, it offers an extension for IQueryable called Search. Therefore, when performing a full-text search, I must write

 myQuery.Search(x=>x.SearchField, searchString)

instead of the Where clause. That's why I am curious if there is any way to override the default predicate builder? I am doing this now (this is a very simple 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 this a workaround? Or is there a more obvious solution that I could not find in the docs?

+4
1

.

, Kendo MVC Wrappers - , . : Kendo.Mvc.Extensions.FilterTokenExtensions , , .

github: KendoGridBinderEx, FilterObject.cs

:

switch (op)
{
    ...

    case "contains":
        exStr = string.Format("{0}{2}.Contains({1})", field, param, caseMod);
        break;

    ...

switch (op)
{
    ...

    case "contains":
        exStr = string.Format("{0}{2}.Search({1})", field, param, caseMod);
        break;

    ...

, , .

+3

All Articles