2

我有一个用户定义的列表

public class Level2
{
    public double price { get; set; }
    public int size { get; set; }

    public Level2(double price, int size)
    {
        this.price = price;
        this.size = size;
    }
}

在我的程序中,我有这个片段,我循环遍历前 10 个元素

List<Level2> bid = new List<Level2>();

        for (int i = 0; i < 10; i++)
        {
            if (i < bid.Count && bid[i].price > bid[0].price - (20 * process.tickSize))
            {
                bidString = bidString + "," + bid[i].price.ToString() + "," + bid[i].size.ToString();
            }                
        }

它编译并运行良好。现在我需要扩展我的程序并想将我的变量更改为数组类型,如下所示:

 List<Level2>[] bid = new List<Level2>[5];

我怎样才能改变我的循环,所以我可以循环通过第一个数组,即bid[0]?

请提供一些工作片段,非常感谢

4

3 回答 3

1

将所有出价替换为出价[0]

List<Level2>[] bid = new List<Level2>[5]; 

    for (int i = 0; i < 10; i++) 
    { 
        if (i < bid[0].Count && bid[0][i].price > bid[0][0].price - (20 * process.tickSize)) 
        { 
            bidString = bidString + "," + bid[0][i].price.ToString() + "," + bid[0][i].size.ToString(); 
        }                 
    } 

或者为出价数组使用不同的变量

List<Level2>[] bidArray = new List<Level2>[5];

然后将 bid 指定为第一个元素

List<Level2> bid = bidArray[0];

然后继续使用现有代码。

于 2012-07-16T16:07:44.157 回答
0

为什么不这样使用 LINQ?

List<Level2>[] bidArray = new List<Level2>()[5];

List<Level2> bid = bidArray[0];
string bidstring = "";

double initialBid = (bid.ToArray())[0].price - (20 * process.tickSize);
IEnumerable<Level2> results = bid.Where( b => b.price > initialBid );

foreach( Level2 item in results ) {
    bidString += "," + item.price.ToString() + "," + item.size.ToString();
}
于 2012-07-16T16:06:18.187 回答
0
List<Level2>[] bid2 = new List<Level2>[5];

foreach (List<Level2> level2List in bid2)
{   
    foreach (Level2 level2Item in level2List)
    {
    }
}

我希望它应该适用于您的情况。

于 2012-07-16T16:27:38.940 回答