我想通过使用语法 LINQ 查询对象从下面的代码示例中获得相同的结果。请帮帮我。
public static List<OHLC> OHLC(List<FileRaw> fileRawsList, TimeSpan timeSpan, DateTime beginDate, DateTime endOfLoopDate)
{
List<OHLC> ohlcList = new List<OHLC>();
for (DateTime i = beginDate; i < endOfLoopDate; )
{
DateTime iLow = i;
DateTime iMax = i.Add(timeSpan);
OHLC ohlcRaw = new OHLC()
{
Open = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().First(),
High = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().Max(),
Low = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().Min(),
Close = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Bid).DefaultIfEmpty().Last(),
Time = fileRawsList.Where(p => p.Time >= iLow).Where(p => p.Time <= iMax).Select(p => p.Time).DefaultIfEmpty(iLow).First()
};
i = i.Add(timeSpan);
ohlcList.Add(ohlcRaw);
}
return ohlcList;
}
它产生
Open: 1.229570, High: 1.233580, Low: 1.221730, Close: 1.224630, Date: 01/08/2012 00:00:02
Open: 1.224620, High: 1.240460, Low: 1.213320, Close: 1.217490, Date: 02/08/2012 00:00:03
Open: 1.217530, High: 1.239160, Low: 1.217230, Close: 1.238650, Date: 03/08/2012 00:00:02
Open: 0, High: 0, Low: 0, Close: 0, Date: 04/08/2012 00:00:02
Open: 1.239720, High: 1.239830, Low: 1.239180, Close: 1.239460, Date: 05/08/2012 17:00:03