/// <summary> /// Initializes a new instance of the <see cref="T:RSql4Net.Models.Paging.Page`1" /> class. /// </summary> /// <param name="content">Content.</param> /// <param name="irSqlPageable">Pageable.</param> /// <param name="total">Total.</param> public RSqlPage(List <T> content, IRSqlPageable <T> irSqlPageable = null, long?total = null) { Content = content ?? new List <T>(); if (total == null) { TotalElements = Content.Count; } else { TotalElements = total.Value < Content.Count ? Content.Count : total.Value; } HasContent = Content.Count > 0; if (HasContent) { NumberOfElements = Content.Count; } if (irSqlPageable == null) { return; } Number = irSqlPageable.PageNumber(); HasPrevious = irSqlPageable.PageNumber() > 0; HasNext = TotalElements > NumberOfElements + (Number * irSqlPageable.PageSize()); var mod = (int)TotalElements % irSqlPageable.PageSize(); var quo = (int)TotalElements - mod; TotalPages = (quo / irSqlPageable.PageSize()) + (mod > 0 ? 1 : 0); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="query"></param> /// <param name="pageable"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static IRSqlPage <T> Page <T>(this IQueryable <T> obj, IRSqlPageable <T> pageable, IRSqlQuery <T> query = null) where T : class { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } if (pageable == null) { throw new ArgumentNullException(nameof(pageable)); } var where = query == null?RSqlQueryExpressionHelper.True <T>() : query.Value(); var count = obj.Count(where); IOrderedQueryable <T> sorted = null; if (pageable.Sort() != null) { var sort = pageable.Sort(); sorted = sort.IsDescending ? obj.OrderByDescending(sort.Value) : obj.OrderBy(sort.Value); sort = sort.Next; while (sort != null) { sorted = sort.IsDescending ? sorted.ThenByDescending(sort.Value) : sorted.ThenBy(sort.Value); sort = sort.Next; } } var offset = pageable.PageNumber() * pageable.PageSize(); var limit = pageable.PageSize(); var result = (sorted ?? obj).Where(where) .Skip(offset) .Take(limit) .ToList(); return(new RSqlPage <T>(result, pageable, count)); }