如何使用ASP.NET MVC 5和ASP.NET运行按比例分配 [英] How to run proration using ASP.NET MVC 5 and ASP.NET
问题描述
这是两个表,
amounttbl, amountId - amountValue
requesttbl reqId - reqAmount - Rate - soldAmount, amountId
数据:
amounttbl
amountId | amountValue
---------|------------
1 | 5000
requesttbl
reqId | reqAmount | Rate | soldAmount | amountId
------|-----------|------|------------|---------
1| 2000| 12| 0| 1
2| 500| 12| 0| 1
3| 1000| 11| 0| 1
4| 500| 10| 0| 1
5| 1000| 10| 0| 1
为此,我准备了一个操作,应该以高价和低价卖出5000个量,在这里它将从(1-2-3)卖出,总计为4500,将保持为500.现在它应该分配给(4& 5).首先,它应该针对以下用途:
For this, I have prepared an action that should sell the amount 5000, from a high rate and to a low rate, which here it will sell from (1-2-3) total will be 4500 it will remain 500. Now it should distribute for (4 & 5). First, it should do it for:
4-- 500/1500 = 0.33且对于5-1000/1500 = 0.66
4-- 500/1500 = 0.33 and for 5 -- 1000/1500 = 0.66
4将获得500的0.33%,而5将获得500的0.66%.
4 will get 0.33 percent of 500 and 5 will get 0.66 percent of 500.
为此,我创建了一个动作,但是它有一些问题:
For this I have created an action, but it has some problems:
id = 1;
amount = 5000;
var requests = db.request.Where(a => a.amountId== id).OrderBy(a => a.Rate);
foreach(var item in requests)
{
decimal soldamount = db.request.Where(a => a.amountId== id).Sum(a => a.reqAmount);
decimal available= amount - soldamount ;
while (available>= item.reqAmount)
{
item.soldAmount= item.reqAmount;
}
}
db.SaveChanges();
在这里我遇到两个问题,一个在foreach
内部:
Here I am facing two problems, one inside the foreach
:
已经有一个与此命令相关联的打开的DataReader, 必须先关闭.
There is already an open DataReader associated with this Command which must be closed first.
这是因为:
十进制已售数量= db.request.Where(a => a.amountId == id).Sum(a => a.reqAmount);
decimal soldamount = db.request.Where(a => a.amountId== id).Sum(a => a.reqAmount);
我不知道如何计算记录4、5并设置它们的部分.
And I don't know how to calculate the record 4, 5 and set their part.
推荐答案
好吧,坦率地说,我没有得到您的算法,但是对于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();
这篇关于如何使用ASP.NET MVC 5和ASP.NET运行按比例分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!