简体   繁体   English

我可以使用LINQ重写以下代码吗?

[英]Can I rewrite the following code using LINQ?

the following code compares two lists which are sorted in descending order to find the removed entries. 下面的代码比较两个列表,这些列表按降序排列以找到已删除的条目。

fullorderbook.asks is the previous list of orders which may contain the removed orders. fullorderbook.asks是先前的订单列表,其中可能包含已删除的订单。

orderbook.asks is the currect list of orders which contains the current state. orderbook.asks是包含当前状态的订单确认当期的列表。

So the algorithm simply loops on the fullorderbook.asks and compares the price with the corresponding order price in the orderbook.asks to find if it already exist or deleted or simply shifted down. 所以算法只是循环的fullorderbook.asks和价格与相应的订单价格在比较orderbook.asks发现,如果它已经存在或者已删除或简单地下移。

My question is, How can I rewrite the code using LINQ? 我的问题是,如何使用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);

PS: PS:

The aim of the algorithm is to find the totally removed orders and the shifted down orders (the order position is greater than the orderbook size OBK_SIZE ). 该算法的目的是找到完全删除的订单和下移的订单(订单位置大于订单OBK_SIZE大小OBK_SIZE )。

So I the usage of the IEnumerable.Except extension method will not give the solution as it will return difference without knowing what is cause of this difference (shifting down or removal). 因此,我使用IEnumerable.Except扩展方法将无法给出解决方案,因为它会在不知道造成这种差异的原因(向下移动或移除)的情况下返回差异。 Because if the order is shifted down I have to keep it in the fullorderbook.asks in the right order. 因为如果顺序向下移动,则必须以正确的顺序将其保留在fullorderbook.asks中。

I would say use the Except Operator . 我会说使用Except 运算符

var deleted = fullorderbook.asks.Except(orderbook.asks);

If you don't want the Except to use the Object.Equals method you could provide a IEqualityComparer<T> . 如果您不希望Except使用Object.Equals方法,则可以提供IEqualityComparer<T> For instance if you still only want to compare by price. 例如,如果您仍然只想按价格进行比较。

Apologies I don't have time to give a properly considered response... this might get you started: 抱歉,我没有时间做出适当考虑的回应……这可能会让您入门:

//checks for deliverableNo's existence in sheetDataSource
if(!(sheetDataSource.Any(item => item.DeliverableNo == varItem.DeliverableNo)))
{
sheetDataSource.Add(varItem);
}

I think this is the solution 我认为这是解决方案

        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);
        }
        )));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM