Skip to content

Instantly share code, notes, and snippets.

@ardacetinkaya
Last active April 2, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardacetinkaya/4a66df33096997431159d91b2897b10d to your computer and use it in GitHub Desktop.
Save ardacetinkaya/4a66df33096997431159d91b2897b10d to your computer and use it in GitHub Desktop.
Pagination Example
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
};
}
}
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);
}
}
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