temp value is always show new value instead of old value
Can someone help me with this code its suppose to change the Lft and Rgt
properties on a department in the selected_deparmtents list. The problem
I'm having is getting the old value or previous value in the temp variable
which holds the previous departments Lft and Rgt properties. What it does
is show the updated value on the temp.Lft property which is wrong I want
the previous Lft property to do the calulation. Does anyone know how I can
get around this problem
int counter = 0;
int lft = department.Lft;
int rgt;
Department temp;
List<Department> clones = new List<Department>(selected_departments);
foreach (Department dept in selected_departments)
{
if (counter < 1)
{
rgt = (dept.Rgt - dept.Lft);
dept.Lft = lft;
dept.Rgt = lft + rgt;
}
else
{
temp = clones.ElementAt(counter - 1); // <-- incorrect
// previous departments value should be old value
lft = lft + (dept.Lft - temp.Lft);// here temp.Lft always show the
newly updated value
rgt = lft + (dept.Rgt - dept.Lft);
dept.Lft = lft;
dept.Rgt = rgt;
}
db.Entry(dept).State = EntityState.Modified;
db.SaveChanges();
counter++;
}
No comments:
Post a Comment