Last active
April 2, 2019 06:45
-
-
Save ardacetinkaya/4a66df33096997431159d91b2897b10d to your computer and use it in GitHub Desktop.
Pagination Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pagination | |
{ | |
public class Result | |
{ | |
public int TotalRecords { get; set; } | |
public int PageSize { get; set; } | |
public int TotalPages { get; set; } | |
public int CurrentPage { get; set; } | |
public int NextPage { get; set; } | |
public int PreviousPage { get; set; } | |
public object Data { get; set; } | |
} | |
/// <summary> | |
/// Creates an output with paging info and paged-sized data | |
/// </summary> | |
/// <typeparam name="T">TotalCount property for proper paging info.If the property does not have that, no output is going to be generated.</typeparam> | |
/// <param name="list">Data list to be paged</param> | |
/// <param name="pageIndex">Page index</param> | |
/// <param name="pagesize">Page size</param> | |
/// <returns></returns> | |
public static object Create<T>(IEnumerable<T> list, int pageIndex, int pagesize) | |
{ | |
int totalCount = 0; | |
int totalPages = 0; | |
int currentPage = 0; | |
int nextPage = 0; | |
int previousPage = 0; | |
if (list != null && list.Count() > 0) | |
{ | |
var propertyInfo = typeof(T).GetProperties().Where(p => p.Name == "TotalCount" && p.PropertyType == typeof(int)).FirstOrDefault(); | |
if (propertyInfo != null) | |
{ | |
totalCount = Convert.ToInt32(propertyInfo.GetValue(list.ToList()[0])); | |
currentPage = Convert.ToInt32(pageIndex); | |
previousPage = (Convert.ToInt32(pageIndex) == 1) ? 0 : (Convert.ToInt32(pageIndex) - 1); | |
nextPage = (Convert.ToInt32(pageIndex) == totalPages) ? 0 : (Convert.ToInt32(pageIndex) + 1); | |
totalPages = (totalCount + pagesize - 1) / pagesize; | |
pagesize = (totalPages == 0) ? 0 : pagesize; | |
if (nextPage > totalPages) nextPage = 0; | |
} | |
} | |
return new Result() | |
{ | |
TotalRecords = totalCount, | |
PageSize = pagesize, | |
TotalPages = totalPages, | |
CurrentPage = pageIndex, | |
NextPage = nextPage, | |
PreviousPage = previousPage, | |
Data = list | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleData | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
/// <summary> | |
/// Total count of the records | |
/// </summary> | |
internal int TotalCount { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var index = 0; //Take this from some external source(UI, Querystring...etc.) | |
var pageSize = 10; //Take this from some external source(UI, Querystring...etc.) | |
var data = new List<ExampleData>();//Some data fetched from SQL | |
//So result is going to be paged result | |
var result = Pagination.Create<ExampleData>(data, index, pageSize); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE PROCEDURE [dbo].[somekind_of_SP_to_list_data] | |
@index INT=0, | |
@pagesize INT=10 | |
AS | |
BEGIN | |
SET NOCOUNT ON | |
SELECT | |
....... | |
,..... | |
,.... | |
,... | |
,.. | |
,. | |
,totalcount = COUNT(*) OVER() | |
FROM sometable AS t | |
ORDER BY 1 | |
OFFSET @start ROW | |
FETCH NEXT @pagesize ROWS ONLY | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment