Exemplo n.º 1
0
        private void shiftIndexes(Category entry, int oldIndex, System.Data.Entity.DbSet <Category> dbset)
        {
            if (entry.Index - oldIndex == 1)
            {
                //Move forward by 1 step
                try
                {
                    dbset.First(x => x.Index == entry.Index).Index = oldIndex; //If was last row - it will throw an exception
                }
                catch (Exception e)
                { }
            }
            else if (entry.Index > oldIndex)
            {
                //We moved forward
                foreach (var q in dbset.ToList())
                {
                    if (oldIndex < q.Index && q.Index < entry.Index)
                    {
                        q.Index = q.Index - 1;
                    }
                }
                entry.Index = entry.Index - 1;
            }
            else if (entry.Index < oldIndex)
            {
                //We moved backwards
                foreach (var q in dbset.ToList())
                {
                    if (entry.Index <= q.Index && q.Index < oldIndex)
                    {
                        q.Index = q.Index + 1;
                    }
                }
            }

            if (entry.Index > dbset.Count())
            {
                entry.Index = dbset.Count();
            }
            else if (entry.Index < 1)
            {
                entry.Index = 1;
            }
        }
Exemplo n.º 2
0
        public virtual List <T> QueryByPage <TKey>(int pageindex
                                                   , int pagesize
                                                   , out int totalcount
                                                   , Expression <Func <T, bool> > where
                                                   , Expression <Func <T, TKey> > order)
        {
            //1.0 计算跳过的总行数
            int skipCount = (pageindex - 1) * pagesize;

            //2.0 获取满足条件的总行数
            totalcount = _dbset.Count(where);

            //3.0 查询
            return(_dbset.Where(where).OrderByDescending(order).Skip(skipCount).Take(pagesize).ToList());
        }
Exemplo n.º 3
0
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Find(id);

            System.Data.Entity.DbSet <Category> dbset = db.Categories;
            int oldIndex = category.Index;

            category.Index = dbset.Count() + 100;

            shiftIndexes(category, oldIndex, dbset);

            db.Categories.Remove(category);
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }