Example #1
0
        public static Option <Page <T> > GetPage <T>(this Microsoft.EntityFrameworkCore.DbSet <T> List, int Page_index, int page_size, Func <T, object> order_by_selector, Func <T, bool> filter_by_selector, bool descending)
            where T : class
        {
            IOrderedEnumerable <T> resTMP;

            if (descending)
            {
                resTMP = List
                         .Where(filter_by_selector)
                         .OrderByDescending(order_by_selector);
            }
            else
            {
                resTMP = List
                         .Where(filter_by_selector)
                         .OrderBy(order_by_selector);
            }

            T[] res = resTMP
                      .Skip(Page_index * page_size)
                      .Take(page_size)
                      .ToArray();

            if (res == null || res.Length == 0)
            {
                return(new Empty <Page <T> >());
            }

            var tot_items = resTMP.Count();

            var tot_pages = tot_items / page_size;

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

            return(new Some <Page <T> >(new Page <T>()
            {
                Index = Page_index, Items = res, TotalPages = tot_pages
            }));
        }
Example #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
            });
        }
Example #3
0
 public IActionResult Get(int id)
 {
     return(Ok(_bikes.Where(p => p.Id == id).FirstOrDefault()));
 }
 /// <summary>
 /// Find the entity for the specified predicate.
 /// </summary>
 /// <param name="predicate">The predicate.</param>
 /// <returns></returns>
 public IEnumerable <T> Find(Expression <Func <T, bool> > predicate)
 {
     return(_dbSet.Where(predicate));
 }