创建一个子矩阵

时间:2012-11-15 00:03:56

标签: c# datetime

我想创建以下矩阵的SubMatrixArrayList(我将其缩小以适应),其中tradeTime> sessionBegin和< SESSIONEND。

DateTime tradeTime;
private DateTime sessionBegin;
private DateTime sessionEnd;

int[,] hTrades =
{
    { 2012, 11, 9, 10, 24, 1, -1, 8515 }, 
    { 2012, 11, 9, 10, 24, 2, -1, 8515 }, 
    { 2012, 11, 9, 10, 44, 3, 1, 8530 }, 
    { 2012, 11, 9, 10, 44, 4, 1, 8535 }, 
    { 2012, 11, 9, 10, 55, 5, -1, 8537 }, 
    { 2012, 11, 9, 10, 55, 6, -1, 8537 }, 
    { 2012, 11, 9, 11, 1, 7, 1, 8552 }, 
    { 2012, 11, 9, 11, 7, 8, 1, 8556 }, 
    { 2012, 11, 11, 18, 18, 1, 1, 8617 }, 
    { 2012, 11, 11, 18, 18, 2, 1, 8617 }, 
    { 2012, 11, 11, 18, 18, 3, 1, 8617 }, 
    { 2012, 11, 11, 18, 18, 4, 1, 8617 }
};

tradeTime = new DateTime(hTrades[i, 0], hTrades[i, 1], hTrades[i, 2],
    hTrades[i, 3], hTrades[i, 4], 0);

1 个答案:

答案 0 :(得分:1)

首先,处理这个二维数组可能会有点尴尬。为简化起见,您可以将该矩阵的每一行分组为一个对象:

class Trade
{
    public DateTime Date { get; set; }
    public int Value1 { get; set; }
    public int Value2 { get; set; }
}

....

DateTime sessionBegin = new DateTime(2012, 11, 9, 11, 0, 0);
DateTime sessionEnd = new DateTime(2012, 11, 9, 12, 0, 0);
List<Trade> trades = new List<Trade>();

for(int i = 0; i < hTrades.GetLength(0); i++)
{
    trades.Add(new Trade()
    {
        Date = new DateTime(hTrades[i, 0], hTrades[i, 1], hTrades[i, 2], hTrades[i, 3], hTrades[i, 4], 0),
        Value1 = hTrades[i, 5],
        Value2 = hTrades[i, 6]
    });
}

var sessionTrades = trades.Where(t => t.Date > sessionBegin && t.Date <= sessionEnd);

这将找到与以下交易对应的Trade个对象:

{ 2012, 11, 9, 11, 1, 7, 1, 8552 },
{ 2012, 11, 9, 11, 7, 8, 1, 8556 }