I have a following legacy code chunk, that I need to rewrite in c#:
Dim dif As Integer
Dim rstTemp As ADODB.Recordset
Set rstTemp = database.OpenRecordset("select * from TABLE order by VALUE desc")
Set dif = <input>
Do Until dif = 0
rstTemp.Fields("VALUE") = rstTemp.Fields("VALUE") - 1
rstTemp.Update
rstTemp.MoveNext
dif = dif - 1
Loop
There is no check for rstTemp.EOF, so it looks to me, that the update changes the iterating order during iteration and it always jumps to the first one? The dif can be a lot bigger, than the number of records, so it has to loop itself somehow. Would this C# code be equivalent?
int dif = <input>;
while (dif != 0)
{
var record = _context.TABLE.OrderByDescending(x => x.VALUE).First();
record.VALUE--;
_context.SaveChanges();
dif--;
}