Skip to content

Instantly share code, notes, and snippets.

@wakeup5
Last active October 12, 2020 01:27
Show Gist options
  • Save wakeup5/571aafa7e54ef4c790a2d4cfd864ce7d to your computer and use it in GitHub Desktop.
Save wakeup5/571aafa7e54ef4c790a2d4cfd864ce7d to your computer and use it in GitHub Desktop.
C# IEnumerable<T>를 페이징 하는 확장 메서드
public struct Page<T> : IEnumerable<T>
{
private IEnumerable<T> source;
private IEnumerable<T> items;
private int pageIndex;
private int countPerPage;
private int maxPage;
public Page(IEnumerable<T> source, int pageIndex, int countPerPage)
{
this.source = source;
this.items = source.Skip(pageIndex * countPerPage).Take(countPerPage);
this.pageIndex = pageIndex;
this.countPerPage = countPerPage;
this.maxPage = (int)Math.Ceiling(source.Count() / (double)countPerPage);
}
public int PageIndex => pageIndex;
public int PageNumber => pageIndex + 1;
public int CountPerPage => countPerPage;
public int MaxPage => maxPage;
public bool IsFirstPage => PageIndex == 0;
public bool IsLastPage => PageNumber == MaxPage;
public Page<T> GetNextPage()
{
if (IsLastPage)
{
return this;
}
return new Page<T>(source, pageIndex + 1, countPerPage);
}
public Page<T> GetPrevPage()
{
if (IsFirstPage)
{
return this;
}
return new Page<T>(source, pageIndex - 1, countPerPage);
}
public IEnumerator<T> GetEnumerator()
{
return items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return items.GetEnumerator();
}
}
public static class PaginationExtensions
{
public static IEnumerable<Page<T>> GetPages<T>(this IEnumerable<T> source, int countPerPage)
{
return Enumerable.Range(0, source.GetPageCount(countPerPage)).Select(index => new Page<T>(source, index, countPerPage));
}
public static Page<T> GetPage<T>(this IEnumerable<T> source, int currentPage, int countPerPage)
{
return new Page<T>(source, currentPage, countPerPage);
}
public static int GetPageCount<T>(this IEnumerable<T> source, int countPerPage)
{
return (int)Math.Ceiling(source.Count() / (double)countPerPage);
}
}
namespace CSharp
{
class Program
{
static void Main(string[] args)
{
List<Item> items = new List<Item>()
{
new Item("1", 10),
new Item("2", 1),
new Item("3", 1),
new Item("4", 1),
new Item("5", 1),
new Item("6", 106),
new Item("7", 1),
new Item("8", 1),
new Item("9", 1),
new Item("0", 1),
new Item("1", 10),
new Item("2", 1),
new Item("3", 1),
new Item("4", 1),
new Item("5", 1),
new Item("6", 106),
new Item("7", 1),
new Item("8", 1),
new Item("9", 1),
new Item("0", 1),
new Item("1", 10),
new Item("2", 1),
new Item("3", 1),
new Item("4", 1),
new Item("5", 1),
new Item("6", 106),
new Item("7", 1),
new Item("8", 1),
new Item("9", 1),
new Item("0", 1),
new Item("1", 10),
new Item("2", 1),
new Item("3", 1),
new Item("4", 1),
new Item("5", 1),
new Item("6", 106),
new Item("7", 1),
new Item("8", 1),
new Item("9", 1),
new Item("0", 1),
};
foreach (var page in items.GetPages(countPerPage: 9))
{
Console.WriteLine($"Page - {page.PageNumber} -");
foreach (var item in page)
{
Console.WriteLine($"{item.name}:{item.stack}");
}
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment