Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以用LINQ重写下面的代码吗?_C#_Linq - Fatal编程技术网

C# 我可以用LINQ重写下面的代码吗?

C# 我可以用LINQ重写下面的代码吗?,c#,linq,C#,Linq,下面的代码比较两个按降序排序的列表,以查找删除的条目 fullorderbook.asks是以前可能包含已删除订单的订单列表 orderbook.asks是包含当前状态的当前订单列表 因此,该算法只是在fullorderbook.asks上循环,并将价格与orderbook.asks中相应的订单价格进行比较,以确定该价格是否已经存在或已被删除或只是下移 我的问题是,如何使用LINQ重写代码 for (int i = 1; i <= fulllength; i++) {

下面的代码比较两个按降序排序的列表,以查找删除的条目

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);
for(int i=1;i=asklength&&asklength=asklength)
{
shiftedorders.Add(orderbook.asks[i]);
}
}
}
fullorderbook.asks.Clear();
fullorderbook.asks.AddRange(orderbook.asks.ToList());
fullorderbook.asks.AddRange(移位订单);
附言:

该算法的目的是寻找完全删除的顺序和下移的顺序 (订单位置大于订单簿大小
OBK_size

因此,我建议使用
IEnumerable.Except
扩展方法不会给出解决方案,因为它将返回差异,而不知道这种差异的原因(下移或删除)。 因为如果订单向下移动,我必须将其保存在
完整的订单簿中。按正确的顺序询问。

我会说使用


如果不希望Except使用Object.Equals方法,可以提供一个
IEqualityComparer
。例如,如果您仍然只想按价格进行比较。

抱歉,我没有时间给出经过适当考虑的答复。。。这可能会让您开始:

//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);
        }
        )));
double highestprice=orderbook.asks[asklength]。价格;
List difflist=新列表(fullorderbook.asks.Except(orderbook.asks));
fullorderbook.asks.Clear();
fullorderbook.asks.AddRange(orderbook.asks);
//补齐订单
fullorderbook.asks.AddRange(
(difflist.FindAll((x)=>
{
//移位顺序
返回x.price>highestprice;
}
)));
//填写已删除的订单
changes.AddRange(
(difflist.FindAll((x)=>
{
//已删除订单
返回x.价格<最高价格;
}).ConvertAll((y)=>
{
返回新CapturedLevel(0,y.价格,-1*y.数量);
}
)));

<>代码>我想你没有读过这个算法,它不是简单地提取差异,而是把它当作删除!!我发现你的算法不太容易阅读,我使用了你的文本解释。我认为,有时候当你想解决一个问题时,你会继续编码直到它工作,而你当前的算法就是这样的结果。您可以尝试更好地解释逻辑,以及2个列表。也许您可以发布完整的工作示例。。。
        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);
        }
        )));