以下代码比较两个按降序排序的列表,以查找删除的条目。
fullorderbook.asks
是以前的订单列表,其中可能包含已删除的订单。
orderbook.asks
是包含当前状态的当前订单列表。
因此,该算法只需在fullorderbook.asks
上循环,并将价格与orderbook.asks
中的相应订单价格进行比较,以查找它是否已存在或已删除或只是向下移动。
我的问题是,如何使用LINQ重写代码?
for (int i = 1; i <= fulllength; i++)
{
withinrange = false;
//for each level in the ask book check if the price still
//exists or it is deleted or just shift down
while (askindex <= asklength)
{
//removed ask
if (orderbook.asks[askindex].price > fullorderbook.asks[i].price)
{
changes.Add(new CapturedLevel(i, fullorderbook.asks[i].price, -1 * fullorderbook.asks[i].volume));
withinrange = true;
break;
}
else if (orderbook.asks[askindex].price < fullorderbook.asks[i].price)
{
//update the ask pointer
askindex++;
}
else
{
withinrange = true;
askindex++;
break;
}
}
if (!withinrange)
{
//removed ask
if (askindex >= asklength && asklength < OBK_SIZE)
{
changes.Add(new CapturedLevel(i, fullorderbook.asks[i].price, -1 * fullorderbook.asks[i].volume));
}
else if (askindex >= asklength)
{
shiftedorders.Add(orderbook.asks[i]);
}
}
}
fullorderbook.asks.Clear();
fullorderbook.asks.AddRange(orderbook.asks.ToList<PriceLevel>());
fullorderbook.asks.AddRange(shiftedorders);
P.S:
该算法的目的是找到完全删除的订单和转移的订单
(订单位置大于订单簿大小OBK_SIZE
)。
所以我使用IEnumerable.Except
扩展方法不会给出解决方案,因为它会在不知道造成这种差异的原因(向下移动或移除)的情况下返回差异。
因为如果订单向下移动,我必须按照正确的顺序将其保留在fullorderbook.asks
中。
答案 0 :(得分:2)
var deleted = fullorderbook.asks.Except(orderbook.asks);
如果您不希望Except使用Object.Equals方法,则可以提供IEqualityComparer<T>
。例如,如果您仍然只想按价格进行比较。
答案 1 :(得分:0)
道歉我没有时间给予适当考虑的回复......这可能会让你开始:
//checks for deliverableNo's existence in sheetDataSource
if(!(sheetDataSource.Any(item => item.DeliverableNo == varItem.DeliverableNo)))
{
sheetDataSource.Add(varItem);
}
答案 2 :(得分:0)
我认为这是解决方案
double highestprice = orderbook.asks[asklength].price;
List<PriceLevel> difflist = new List<PriceLevel>(fullorderbook.asks.Except(orderbook.asks));
fullorderbook.asks.Clear();
fullorderbook.asks.AddRange(orderbook.asks);
//fill the shifted orders
fullorderbook.asks.AddRange(
(difflist.FindAll((x) =>
{
//shifted order
return x.price > highestprice;
}
)));
//fill the deleted orders
changes.AddRange(
(difflist.FindAll((x) =>
{
//deleted order
return x.price < highestprice;
}).ConvertAll((y)=>
{
return new CapturedLevel(0, y.price, -1*y.volume);
}
)));