using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NBAManagment.Filter { public class PageFilter : IFilter { public int CurrentPageNum { get; set; } public int PageSize { get; set; } public PageFilter(int pageSize, int currentPageNum) { this.PageSize = pageSize; this.CurrentPageNum = currentPageNum; } public int GetPageCount(IEnumerable collection) { var itemsCount = collection.Count(); var incompletePageCount = (itemsCount % PageSize) > 0 ? 1 : 0; return itemsCount / PageSize + incompletePageCount; } private bool IsNumInPage(int pageNum) { int firstElementOnCurrentPageNum = PageSize * (CurrentPageNum - 1) + 1; return pageNum >= firstElementOnCurrentPageNum && pageNum <= (firstElementOnCurrentPageNum + PageSize - 1); } public IEnumerable Use(IEnumerable collection) { bool elementsFind = false; int currentPageNum = 1; var resultCollection = new List(); foreach (var item in collection) { if (IsNumInPage(currentPageNum)) { resultCollection.Add(item); elementsFind = true; } else if (elementsFind) { break; } currentPageNum++; } return resultCollection; } } }