.
假设我有一个List<Type>
和一个具有以下属性的Type
:
public DateTime OriginalDateTime { get; set; }
public double RoundedTime { get; }
public int MyValue { get; set; }
该类包含一个构造函数:
public Match(DateTimeOffset dateTime, int bid, int ask, int volume)
{
DateTime = dateTime;
RoundedTime = Math.Round(Math.Ceiling(dateTime.ToTimeStamp() / 60) * 60); // Round to nearest minute
Bid = bid;
Ask = ask;
Volume = volume;
}
现在使用 LINQ
和 GroupBy
我得到了按 RoundedTime
分组的所有结果。没问题,但是,我想检查每分钟是否有结果(因此对于一小时的数据,GroupBy
应返回 60 个结果(。
最好,当LINQ
结果中缺少一分钟时,我想使用以前的值(假设缺少第 30 分钟,用第 29 分钟的数据填充它(。
现在我想知道这是否可能与LINQ
我想你想实现这个:
IEnumerable<IGrouping<int, MyType>> mygrouping = new List<MyType>().GroupBy(x => x.Key); // this is your grouped list
var allData = Enumerable.Range(0, 60) // here you take 60 values (0-59)
.Select(
actualKey =>
{
var p = mygrouping.SingleOrDefault(y => y.Key == actualKey);
int counter = actualKey;
while (p == null && counter >= 0) // as long as the value is null and the counter is greater or equal to 0
p = mygrouping.SingleOrDefault(y => y.Key == counter--);
return new KeyValuePair<int, IEnumerable<MyType>>(actualKey, p.Select(v => v)); // return the data of the last value
});
这只是一点帮助,我想您需要修改代码以使其与您的代码一起使用。并且:我猜还有更多性能的方式