/// <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); }