如何获得在LINQ最高和最低价格的项目的数量的总和 [英] How to get the sum of the volume of highest and lowest priced items in linq

查看:207
本文介绍了如何获得在LINQ最高和最低价格的项目的数量的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写的实际查询稍微棘手比标题所暗示的。我有一个像这样的订单列表:列表<排序> ,顺序是这样的:

The actual query I am trying to write is slightly trickier than the title suggests. I have a list of orders like such: List<Order>, an order looks like this:

public class Order
{
    private StockCodes _stockCode;
    private bool _bidSide;
    private int _volume;
    private decimal _price;
}



我需要发布的最佳买入价量和最佳的卖出价和量给出了具体的股票代码。最好的买入价被定义为bidSide是真实的最高价格。最好的卖出价被定义为bidSide是假的最低价格。

I need to publish the best bid price and volume and the best sell price and volume given a specific stock code. The best bid price is defined as the HIGHEST price where bidSide is true. The best sell price is defined as the LOWEST price where bidSide is false.

例如给出的股票代码为ABC以下数据:

For example given the following data for stock code "ABC":

 { bidSide: true, volume: 25, price: 25  }
 { bidSide: true, volume: 25, price: 25  }
 { bidSide: true, volume: 25, price: 5  }

 { bidSide: false, volume: 100, price: 1  }
 { bidSide: false, volume: 50, price: 2}
 { bidSide: false, volume: 75, price: 8 }

最佳买入:价格25,体积50(因为有2个数量最高价格)
最好卖:售价1,成交量100(因为以最低的价格只是1级)

Best bid: price 25, volume 50 (since there are 2 orders at the highest price) Best sell: price 1, volume 100 (since there is just 1 order at the lowest price)

最后,我需要考虑时,有没有出价或卖单。效率是高优先级的,所以如果我能做到这一点,将被首选1 LINQ声明。

Lastly, I need to account for when there are no bid or sell orders. Efficiency is of high priority, so If I am able to do this in one linq statement that would be preferred.

推荐答案

要做到这种有效的,你真的只需要遍历一次数据。不幸的是,这使得它与LINQ实现,因为有相当多的工作要做一个真正的痛苦。

To do this efficiently, you really only want to iterate over the data once. Unfortunately, that makes it a real pain to implement with LINQ, as there's quite a lot of work to do.

我个人建议你的的做到这一点使用LINQ - 你的可能的有总结实现它,但它不会是非常愉快的。这不是一个简单的的foreach 环太糟糕了,虽然。是这样的:

Personally I would suggest that you don't do this with LINQ - you could implement it with Aggregate, but it wouldn't be terribly pleasant. It's not too bad with a simple foreach loop though. Something like:

int buyVolume = -1;
int sellVolume = -1;
decimal buyPrice = decimal.MinValue;
decimal sellPrice = decimal.MaxValue;

foreach (var order in orders)
{
    if (order.bidSide)
    {
        if (order.Price > buyPrice)
        {
            buyPrice = order.Price;
            buyVolume = order.Volume;
        }
        else if (order.Price == buyPrice)
        {
            buyVolume += order.Volume;
        }
    }
    else
    {
        if (order.Price < sellPrice)
        {
            sellPrice = order.Price;
            sellVolume = order.Volume;
        }
        else if (order.Price == sellPrice)
        {
            sellVolume += order.Volume;
        }
    }
}

// Check sellVolume == -1 to verify whether we've seen any sale orders
// Check buyVolume == -1 to verify whether we've seen any buy orders
// Use buyPrice/buyVolume and sellPrice/sellVolume otherwise

在LINQ尽可能高效地做这将有效地意味着将的所有的在循环到一个函数,逻辑传递到总结 - 你很可能希望创建一个自定义的值类型来保存四个值,以避免超过你需要创建更多的对象。这的可以的矫枉过正,但你没有说你想让它尽可能高效...

Doing it as efficiently as possible in LINQ would effectively mean putting all that logic in the loop into a function to pass into Aggregate - and you'd probably want to create a custom value type to hold the four values, to avoid creating more objects than you need to. That may be overkill, but you did say you wanted it as efficient as possible...

这篇关于如何获得在LINQ最高和最低价格的项目的数量的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆