My database has a Task
table with a Sequence
column. The Sequence
column specifies the order of the tasks.
In some cases, I need to change the order. So I would use something like this:
var tasks = (dbContext.Tasks
.Where(t => t.UserId == userId)).ToList();
for (int i = 0; i < tasks.Count; i++)
{
// Set new sequence
tasks[i].Sequence = i;
}
dbContext.SaveChanges();
This seems rather inefficient to have to retrieve every column of every Task
in the set.
Is there a more efficient way to do this?
Note: Please don't get caught up in the fact that I'm simply setting Sequence
to i
in the code above. The real code will have data receive data that indicates the correct values. But if I could optimize the code above, I could then adapt it to my final needs.