I am returning the below record set. What i am trying to accomplish is the data posted below. So, I need to update every numberUp records with a formNum that increments. The problem is in this case is that every third record gets skipped it is the third iteration when the code checks if idx == numberUp. What am I missing here? Any help would be greatly appreciated.
DATA BEFORE
Quantity TransitDays Form NumberUp
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
1750 3 0 2
DATA AFTER
Quantity TransitDays Form NumberUp
1750 3 1 2
1750 3 1 2
1750 3 2 2
1750 3 2 2
1750 3 3 2
1750 3 4 2
1750 3 5 2
1750 3 5 2
1750 3 6 2
1750 3 6 2
1750 3 0 2
numberUp = 2;
var formNum = 1;
var idx = 0;
foreach (var qty in quantities)
{
for (int i = transitdays.Count; i > 0; i--)
{
var query = (from runlist in db.Runlists
where runlist.UserId == user.Id && runlist.Quantity == qty.Quantity &&
runlist.TransitDays == i && runlist.Form == 0
orderby runlist.TransitDays descending
select runlist).ToList();
if (query.Count >= numberUp)
{
foreach (var item in query)
{
if (idx < numberUp)
{
item.Form = formNum;
db.SaveChanges();
idx++;
}
else
{
formNum++;
idx = 0;
}
}
}
}
}
CreateTable(
"dbo.Runlist",
c => new
{
Quantity = c.Int(nullable: false),
TransitDays = c.Int(nullable: false),
Form = c.Int(nullable: true),
NumberUp= c.Int(nullable: true)
})