Example #1
0
        public PageData <Customer> GetPage(int pageNumber, int pageSize, string nameFilter, decimal discountStart, decimal discountEnd)
        {
            var query = context.Set <Customer>().AsQueryable();

            if (nameFilter != null)
            {
                query = query.Select(x => x).Where(x => (x.Name.StartsWith(nameFilter)));
            }
            if (discountStart != -1)
            {
                query = query.Select(x => x).Where(x => (x.DiscountPercent >= discountStart));
            }

            if (discountEnd != -1)
            {
                query = query.Select(x => x).Where(x => (x.DiscountPercent <= discountEnd));
            }

            var page = new PageData <Customer>();
            int c    = query.Count();
            int p;

            if (c % pageSize > 0)
            {
                p = c / pageSize + 1;
            }
            else
            {
                p = c / pageSize;
            }
            page.Size = p;
            page.Data = query.OrderBy(x => x.Name).Skip(pageNumber * pageSize).Take(pageSize).ToList();

            return(page);
        }
Example #2
0
        public PageData <Product> GetPage(int pageNumber, int pageSize, string nameFilter, decimal priceStart, decimal priceEnd)
        {
            var query = context.Set <Product>().AsQueryable();

            if (nameFilter != null)
            {
                query = query.Select(x => x).Where(x => (x.Name.StartsWith(nameFilter)));
            }

            if (priceStart != -1)
            {
                query = query.Select(x => x).Where(x => (x.ListPrice >= priceStart));
            }

            if (priceEnd != -1)
            {
                query = query.Select(x => x).Where(x => (x.ListPrice <= priceEnd));
            }

            var page = new PageData <Product>();
            int c    = query.Count();
            int p;

            if (c % pageSize > 0)
            {
                p = c / pageSize + 1;
            }
            else
            {
                p = c / pageSize;
            }

            page.Size = p;
            page.Data = query.OrderBy(x => x.Name).Skip(pageNumber * pageSize).Take(pageSize).ToList();

            return(page);
        }
Example #3
0
        public virtual void Delete(T entity)
        {
            if (entity != null)
            {
                using (var tran = context.Database.BeginTransaction())
                {
                    try
                    {
                        context.Set <T>().Remove(entity);
                        //context.Entry(entity).State = EntityState.Deleted;
                        context.SaveChanges();
                        tran.Commit();
                    }

                    catch
                    {
                        tran.Rollback();
                        context.Entry(entity).State = EntityState.Detached;
                        throw;
                    }
                }
            }
        }