更新时间:2023-02-25 20:19:25
好吧,坦率地说,我没有得到您的算法,但是对于open DataReader
问题,请尝试以下代码:
Well, to be frank I don't get your algo but as for open DataReader
issue try this code:
id = 1;
amount = 5000;
//Get list of objects from db (assuming db is `DbContext` and request is `IDbSet` stuff)
var requests = db.request.Where(a => a.amountId== id).OrderBy(a => a.Rate).ToList();
//we already have requests with amountId == id in *requests* variable above.
//db.request.Where(a => a.amountId== id).Sum(a => a.reqAmount);
//This does not change during loop, so calculate before hand
var soldamount = requests.Sum(a => a.reqAmount);
decimal available = amount - soldamount;
foreach(var item in requests)
{
//don't get concept of while here as you are not changing either
//item.reqAmount not available
if(available>= item.reqAmount)
{
item.soldAmount= item.reqAmount;
}
}
db.SaveChanges();