/// <summary> /// Creates new instance of <see cref="PagedList{T}"/> collection. /// </summary> /// <param name="queryable"><see cref="IDataTablesQueryable{T}"/>instance to be paginated</param> internal PagedList(IDataTablesQueryable <T> queryable) : base() { // pagination is on if (queryable.Request.PageSize > 0) { int skipCount = (queryable.Request.PageNumber - 1) * queryable.Request.PageSize; int takeCount = queryable.Request.PageSize; TotalCount = queryable.Count(); PageNumber = queryable.Request.PageNumber; PageSize = queryable.Request.PageSize; PagesCount = TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1; AddRange(queryable.Skip(skipCount).Take(takeCount).ToList()); } // no pagination else { TotalCount = queryable.Count(); PageNumber = 1; PageSize = -1; PagesCount = 1; AddRange(queryable.ToList()); } }
/// <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 ordering operations defined by <see cref="DataTablesRequest{T}"/>. /// </summary> /// <typeparam name="T">Data type to be ordered</typeparam> /// <param name="queryable"><see cref="IDataTablesQueryable{T}"/> instance to be ordered.</param> /// <returns><see cref="IDataTablesQueryable{T}"/> with appied ordering from <see cref="DataTablesRequest{T}"/></returns> public static IDataTablesQueryable <T> Order <T>(this IDataTablesQueryable <T> queryable) { // orderable columns var columns = queryable.Request.Columns.Where(c => c.IsOrderable && c.OrderingIndex != -1) .OrderBy(c => c.OrderingIndex); bool alreadyOrdered = false; foreach (var c in columns) { queryable = (IDataTablesQueryable <T>)queryable.OrderBy(c.PropertyName, c.OrderingDirection, c.OrderingCaseInsensitive, alreadyOrdered); alreadyOrdered = true; } 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); }
/// <summary> /// Asynchronously creates a <see cref="IPagedList{T}"/> from a <see cref="IDataTablesQueryable{T}"/>. /// </summary> /// <typeparam name="T">Data type.</typeparam> /// <param name="queryable"><see cref="IDataTablesQueryable{T}"/> instance.</param> /// <returns><see cref="IPagedList{T}"/> instance.</returns> public static Task <IPagedList <T> > ToPagedListAsync <T>(this IDataTablesQueryable <T> queryable) { return(Task.Factory.StartNew <IPagedList <T> >(() => new PagedList <T>(queryable))); }
/// <summary> /// Creates a <see cref="IPagedList{T}"/> from a <see cref="IDataTablesQueryable{T}"/>. /// </summary> /// <typeparam name="T">Data type.</typeparam> /// <param name="queryable"><see cref="IDataTablesQueryable{T}"/> instance.</param> /// <returns><see cref="IPagedList{T}"/> instance.</returns> public static IPagedList <T> ToPagedList <T>(this IDataTablesQueryable <T> queryable) { return(new PagedList <T>(queryable)); }
/// <summary> /// Builds a <see cref="PagedList{TEntity, TEntityViewModel}"/> from the given <see cref="IDataTablesQueryable{TEntity, TEntityViewModel}"/>. /// </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 IPagedList <TEntityViewModel> ToPagedList <TEntity, TEntityViewModel>(this IDataTablesQueryable <TEntity, TEntityViewModel> queryable) { return(new PagedList <TEntity, TEntityViewModel>(queryable)); }
/// <summary> /// Applies the requested order for each of the <see cref="DataTablesRequest{TEntity, TEntityViewModel}.Columns"/>. /// </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> /// <returns></returns> public static IDataTablesQueryable <TEntity, TEntityViewModel> ApplyOrder <TEntity, TEntityViewModel>(this IDataTablesQueryable <TEntity, TEntityViewModel> queryable) { var columns = queryable.Request.Columns .Where(c => c.IsOrderable && c.OrderingIndex >= 0) .OrderBy(c => c.OrderingIndex); bool alreadyOrdered = false; foreach (var c in columns) { var propertyName = c.ColumnOrderingProperty != null ? c.ColumnOrderingProperty.GetPropertyPath() : c.PrivatePropertyName; queryable = (IDataTablesQueryable <TEntity, TEntityViewModel>)queryable.OrderBy(propertyName, c.OrderingDirection, c.OrderingCaseInsensitive, alreadyOrdered); alreadyOrdered = true; } return(queryable); }