簡體   English   中英

我可以使用LINQ重寫以下代碼嗎?

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

下面的代碼比較兩個列表,這些列表按降序排列以找到已刪除的條目。

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

PS:

該算法的目的是找到完全刪除的訂單和下移的訂單(訂單位置大於訂單OBK_SIZE大小OBK_SIZE )。

因此,我使用IEnumerable.Except擴展方法將無法給出解決方案,因為它會在不知道造成這種差異的原因(向下移動或移除)的情況下返回差異。 因為如果順序向下移動,則必須以正確的順序將其保留在fullorderbook.asks中。

我會說使用Except 運算符

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

如果您不希望Except使用Object.Equals方法,則可以提供IEqualityComparer<T> 例如,如果您仍然只想按價格進行比較。

抱歉,我沒有時間做出適當考慮的回應……這可能會讓您入門:

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

我認為這是解決方案

        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