Esempio n. 1
0
        /// <summary>
        /// Returns a single page of results from an IOrderedQueryable.
        /// </summary>
        /// <typeparam name="T">The collection type.</typeparam>
        /// <param name="source">The collection to be paged.</param>
        /// <param name="request">An IPageRequest specifying a PageSize and a PageNumber to reduce the collection by.</param>
        /// <returns>the IOrderedQueryable with Skip() and Take() applied.</returns>
        public static IQueryable <T> Page <T>(this IOrderedQueryable <T> source, IPageRequest request)
        {
            if (request != null)
            {
                return(source.Page(request.PageNumber, request.PageSize));
            }

            return(source);
        }
 /// <summary>
 /// Расширение для постраничного вывода коллекции
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="obj">Коллекция</param>
 /// <param name="page">Номер страницы</param>
 /// <param name="pageSize">Количество элементов на странице</param>
 /// <param name="objectsCount">Общее количество объектов в коллекции</param>
 /// <returns></returns>
 public static IQueryable <T> Page <T>(this IOrderedQueryable <T> obj, int page, int pageSize, out int objectsCount)
 {
     objectsCount = obj.Count();
     if (page > 1 && (page - 1) * pageSize >= objectsCount)
     {
         page = (objectsCount - 1) / pageSize + 1;
     }
     return(obj.Page(page, pageSize));
 }
 public static Page <T> ToPage <T>(this IOrderedQueryable <T> source, Query r)
 {
     return(new Page <T>
     {
         PageSize = r.PageSize,
         Count = source.Count(),
         List = source.Page(r).ToList(),
         PageIndex = r.Page
     });
 }
        public static async Task <AiurPagedCollection <T> > BuildAsync <T>(
            IOrderedQueryable <T> query,
            IPageable pager,
            ErrorType code,
            string message)
        {
            var items = await query.Page(pager).ToListAsync();

            return(new AiurPagedCollection <T>(items)
            {
                TotalCount = await query.CountAsync(),
                CurrentPage = pager.PageNumber,
                CurrentPageSize = pager.PageSize,
                Code = code,
                Message = message
            });
        }
Esempio n. 5
0
        public void Entities_Page()
        {
            //Arrange
            const int total    = 33;
            const int page     = 2;
            const int pageSize = 10;

            PopulateTestData(total, 0);

            //Act
            var expected = _context.Blogs.OrderBy(b => b.BlogId).Page(page, pageSize).ToArray();
            IOrderedQueryable queryable = _context.Blogs.Select("it").OrderBy("BlogId");
            bool any    = queryable.Any();
            var  count  = queryable.Count();
            var  result = queryable.Page(page, pageSize).ToDynamicArray <Blog>();

            //Assert
            Assert.Equal(true, any);
            Assert.Equal(total, count);
            Assert.Equal(expected, result);
        }
Esempio n. 6
0
        /// <summary>
        /// Esegue l'operazione per tutte le entità nella in entities, salvando ad ogni pagina
        /// </summary>
        /// <remarks>http://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework/5942176#5942176</remarks>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="context"></param>
        /// <param name="entities"></param>
        /// <param name="action"></param>
        public static void BulkOperation <TEntity>(this DbContext context, IOrderedQueryable <TEntity> entities, Action <TEntity> action, int pagingCount = 100)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(15)))
            {
                var entitiesCount = entities.Count();
                var pagesCount    = (int)Math.Ceiling((decimal)entitiesCount / (decimal)pagingCount);

                for (int i = pagesCount - 1; i >= 0; i--)
                {
                    var pagedEntities = entities.Page(i, pagingCount).ToList();
                    foreach (var entity in pagedEntities)
                    {
                        action(entity);
                    }
                    context.SaveChanges();
                    // clear the entities tracking
                    context.ChangeTracker.Entries().ToList().ForEach(e => e.State = EntityState.Detached);
                }

                scope.Complete();
            }
        }