Swap IEnumerable dataset

Are there built-in swap functions for IEnumberable (or the best library to use)? I know there is Take <> (), but I find that I do basic calculations repeatedly to determine the number of pages for a given page size. I understand that this is a simple implementation, but I hope it is already in the library, and I just skipped it.

By paging, I mean a pointer to the current record and something to fulfill the following concepts.

.PageSize <- get / set page size .Last <- last page Current and current page .JumpTo (PageNumber)

With crash protection to make sure you get to the right place if page size or file resizing

+3
source share
1 answer

You can use a wrapper PagedListaround the Rob Conery list . There is also an extended version of Troy Goode .

using System;
using System.Collections.Generic;
using System.Linq;

namespace System.Web.Mvc
{
    public interface IPagedList
    {
        int TotalCount
        {
            get;
            set;
        }

        int PageIndex
        {
            get;
            set;
        }

        int PageSize
        {
            get;
            set;
        }

        bool IsPreviousPage
        {
            get;
        }

        bool IsNextPage
        {
            get;
        }     
    }

    public class PagedList<T> : List<T>, IPagedList
    {
        public PagedList(IQueryable<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }    

        public PagedList(List<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }

        public int TotalCount      
        { 
            get; set; 
        }

        public int PageIndex       
        { 
            get; set; 
        }

        public int PageSize 
        { 
            get; set; 
        }

        public bool IsPreviousPage 
        { 
            get 
            {
                return (PageIndex > 0);
            }
        }

        public bool IsNextPage 
        { 
            get
            {
                return (PageIndex * PageSize) <=TotalCount;
            } 
        }        
    }

    public static class Pagination
    {
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize) 
        {
            return new PagedList<T>(source, index, pageSize);
        }

        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        {
            return new PagedList<T>(source, index, 10);
        }        
    }
}
+2
source

Source: https://habr.com/ru/post/1735332/


All Articles