Esempio n. 1
0
        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
        {
            var res = list.OrderBy(order_by_selector)
                      .Skip(page_index * page_size)
                      .Take(page_size)
                      .ToArray();

            if (res == null || res.Length == 0)
            {
                return(null);
            }

            var tot_items = list.Count();
            var tot_pages = tot_items / page_size;

            if (tot_items < page_size)
            {
                tot_pages = 1;
            }

            return(new Page <T>()
            {
                Index = page_index, Items = res, TotalPages = tot_pages
            });
        }
Esempio n. 2
0
        public static Page <T> GetPage <T>(this Microsoft.EntityFrameworkCore.DbSet <T> list, int page_index, int page_size, Func <T, object> order_by_selector, bool descending = false, Func <T, bool> filter_by_predicate = null)
            where T : class
        {
            var res = list.OrderBy(order_by_selector)
                      .Skip(page_index * page_size)
                      .Take(page_size)
                      .ToArray();

            if (descending == true)
            {
                res = list.OrderByDescending(order_by_selector)
                      .Skip(page_index * page_size)
                      .Take(page_size)
                      .ToArray();
            }

            var tot_items = list.Count();

            if (descending == true)
            {
                if (filter_by_predicate != null)
                {
                    res = list.Where(filter_by_predicate)
                          .OrderByDescending(order_by_selector)
                          .Skip(page_index * page_size)
                          .Take(page_size)
                          .ToArray();

                    tot_items = list.Where(filter_by_predicate)
                                .Count();
                }
            }
            else
            {
                if (filter_by_predicate != null)
                {
                    res = list.Where(filter_by_predicate)
                          .OrderBy(order_by_selector)
                          .Skip(page_index * page_size)
                          .Take(page_size)
                          .ToArray();

                    tot_items = list.Where(filter_by_predicate)
                                .Count();
                }
            }

            if (res == null || res.Length == 0)
            {
                return(null);
            }


            var tot_pages = tot_items / page_size;

            if (tot_items < page_size)
            {
                tot_pages = 1;
            }

            return(new Page <T>()
            {
                index = page_index, items = res, totalPages = tot_pages
            });
        }