Question

I'm using Kendo UI for ASP.NET MVC with server wrappers.

IQueryable extension method ToDataSourceResult currently creates query like

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

when using Contains operator for filtering.

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?

Was it helpful?

Solution

Interesting question.

I don't think there is any plug-able functionality in Kendo MVC Wrappers to change this. You can take a look at the source code : Kendo.Mvc.Extensions.FilterTokenExtensions and maybe change this.

However you can also modify my project on github : KendoGridBinderEx, take a look at the file FilterObject.cs

Change code:

switch (op)
{
    ...

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

    ...

To

switch (op)
{
    ...

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

    ...

Please let me know if this works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top