private void shiftIndexes(Question entry, int oldIndex, System.Data.Entity.DbSet <Question> 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; } }