public static Page <T> GetPage <T>(this Microsoft.EntityFrameworkCore.DbSet <T> list, int page_index, int page_size, Func <T, object> order_by_selector) where T : class { //the given index and size are used to select the correct data //note the OrderBy which is necessary to ensure the selection in the //correct order var res = list.OrderBy(order_by_selector) .Skip(page_index * page_size) .Take(page_size) .ToArray(); //in case the query fails or the page size is zero a NotFOund is returned if (res == null || res.Length == 0) { return(null); } //we calculate once the total amonut of items of the unput list var tot_items = list.Count(); //we calculate the total amount of pages. incase the page size is greater than //the total amount of items we set the total pages to 1 var tot_pages = tot_items / page_size; if (tot_items < page_size) { tot_pages = 1; } //we group all the date and return then to the caller return(new Page <T>() { Index = page_index, Items = res, TotalPages = tot_pages }); }
public static Page <T> GetPages <T>(this Microsoft.EntityFrameworkCore.DbSet <T> list, int index_page, int page_size, Func <T, object> order_by_selector, params string[] relationsToInclude) where T : class { var result = list.IncludeMultiple(relationsToInclude).OrderBy(order_by_selector) .Skip(index_page * page_size) .Take(page_size) .ToArray(); var tot_items = list.Count(); var tot_pages = tot_items / page_size; return(new Page <T> { Index = index_page, Items = result, TotalPages = tot_pages }); }
public static Page <T> GetPages <T>(this Microsoft.EntityFrameworkCore.DbSet <T> list, int index_page, int page_size, Func <T, object> order_by_selector) where T : class { var result = list.OrderBy(order_by_selector) //index = 1; page_size = 3 , skip pages = 3*1 =3 .Skip(index_page * page_size) .Take(page_size) .ToArray(); var total_items = list.Count(); var total_pages = total_items / page_size; return(new Page <T> { index = index_page, Items = result, TotalPages = total_pages }); }
public static Page <T> GetPages <T>(this Microsoft.EntityFrameworkCore.DbSet <T> list, int index_page, int page_size, Func <T, object> order_by_selector, params string[] relationsToInclude) where T : class { var result = list.IncludeMultiple(relationsToInclude).OrderBy(order_by_selector) .Skip(index_page * page_size) .Take(page_size) .ToArray(); int tot_items = list.Count(); int tot_pages = (int)(Math.Ceiling((double)tot_items / (double)page_size)); // If there are no Items, there will still be 1 page if (tot_items < page_size) { tot_pages = 1; } return(new Page <T> { Index = index_page, Items = result, TotalPages = tot_pages }); }