/// <summary> /// Modifies the <see cref="IDataTablesQueryable{T}"/> by applying custom filter from <see cref="DataTablesRequest{T}"/>. /// </summary> /// <typeparam name="T">Data type to be filtered</typeparam> /// <param name="queryable"><see cref="IDataTablesQueryable{T}"/> instance to be filtered.</param> /// <returns>Modified <see cref="IDataTablesQueryable{T}"/> with applied custom filter.</returns> public static IDataTablesQueryable <T> CustomFilter <T>(this IDataTablesQueryable <T> queryable) { if (queryable.Request.CustomFilterPredicate != null) { queryable = (IDataTablesQueryable <T>)queryable.Where(queryable.Request.CustomFilterPredicate); } return(queryable); }
/// <summary> /// Applies the <see cref="DataTablesRequest{TEntity, TEntityViewModel}.GlobalSearchValue"/> of the given /// <see cref="IDataTablesQueryable{TEntity, TEntityViewModel}"/> to the query, if set. /// To perform the global search, the method will add a search expression for each searchable column of the request. /// </summary> /// <typeparam name="TEntity">The type of the entity.</typeparam> /// <typeparam name="TEntityViewModel">The type of the entity view model.</typeparam> /// <param name="queryable">The queryable.</param> public static IDataTablesQueryable <TEntity, TEntityViewModel> ApplyGlobalSearchFilter <TEntity, TEntityViewModel>( this IDataTablesQueryable <TEntity, TEntityViewModel> queryable) { var globalSearchValue = queryable.Request.GlobalSearchValue; if (!string.IsNullOrEmpty(globalSearchValue)) { var columns = queryable.Request.Columns.Where(c => c.IsSearchable); if (columns.Any()) { Expression <Func <TEntity, bool> > predicate = null; foreach (var c in columns) { Expression <Func <TEntity, bool> > expression; var searchPredicate = c.GlobalSearchPredicate ?? c.SearchPredicate; if (searchPredicate != null) { var expr = searchPredicate; var entityParam = expr.Parameters.Single(p => p.Type == typeof(TEntity)); var searchValueConstant = Expression.Constant(globalSearchValue); expression = (Expression <Func <TEntity, bool> >)Expression.Lambda(Expression.Invoke(expr, entityParam, searchValueConstant), entityParam); } else { if (queryable.Request.GlobalSearchRegex) { expression = ExpressionHelper.BuildRegexPredicate <TEntity>(c.PrivatePropertyName, globalSearchValue); } else { expression = ExpressionHelper.BuildStringContainsPredicate <TEntity>(c.PrivatePropertyName, globalSearchValue, c.SearchCaseInsensitive); } } predicate = predicate == null ? PredicateBuilder.Create(expression) : predicate.Or(expression); } queryable = (IDataTablesQueryable <TEntity, TEntityViewModel>)queryable.Where(predicate); } } return(queryable); }
/// <summary> /// Modifies the <see cref="IDataTablesQueryable{T}"/> by applying individual column search from <see cref="DataTablesRequest{T}"/>. /// </summary> /// <typeparam name="T">Data type to be filtered</typeparam> /// <param name="queryable"><see cref="IDataTablesQueryable{T}"/> instance to be filtered.</param> /// <returns><see cref="IDataTablesQueryable{T}"/> with appied individual column search from <see cref="DataTablesRequest{T}"/></returns> public static IDataTablesQueryable <T> ColumnsSearch <T>(this IDataTablesQueryable <T> queryable) { // searchable columns var columns = queryable.Request.Columns.Where(c => c.IsSearchable && !String.IsNullOrEmpty(c.SearchValue)); if (columns.Any()) { Expression <Func <T, bool> > predicate = null; foreach (var c in columns) { var expr = c.ColumnSearchPredicate ?? BuildStringContainsPredicate <T>(c.PropertyName, c.SearchValue, c.SearchCaseInsensitive); predicate = predicate == null? PredicateBuilder.Create(expr) : predicate.And(expr); } queryable = (IDataTablesQueryable <T>)queryable.Where(predicate); } return(queryable); }