0

以下查询似乎工作得很好:

var query =
Session.Query<SomeEvent>()
    .Where(
       p =>
       p.Timestamp >= from.Now
       && p.Timestamp <= to.Now
    )
    .GroupBy(x => x.Timestamp)
    .AsEnumerable()
    .Select(
    x =>
    new SomeEvent {Timestamp = x.Key, DisplayValue = x.Average(y => y.DisplayValue)})
    .OrderBy(x => x.Timestamp);

如果在时间戳(日期时间)方面存在重复,则平均显示值。我只是好奇是否也可以根据“分辨率”(例如 30 分钟)对值进行平均?

4

1 回答 1

1

这个怎么样?我介绍了一个称为分辨率级别的概念,它为每个时间戳差异范围提供了一个级别的密钥:1 如果在 30 分钟内,2 如果在 60 分钟内,3 如果在 90 分钟内......等等。基于这些级别,查询可以平均每个

double resolutionInMins = 30; // In minites
double divisor = to.Now.AddMinutes(resolutionInMins).Ticks - to.Now.Ticks;

Func<DateTime, double> resolutionLevel = 
    delegate(DateTime timestamp)
{
    return Math.Ceiling((to.Now.Ticks - timestamp.Ticks) / divisor);
};

var query =
    Session.Query<SomeEvent>()
    .Where(p => 
        p.Timestamp >= from.Now && 
        p.Timestamp <= to.Now)
.GroupBy(x => new
{
    ResolutionLevel = resolutionLevel(x.Timestamp)
})
.Select(x => new
{
    ResolutionLevel = x.Key.ResolutionLevel,
    ResolutionAvgValue = x.Average(b => b.DisplayValue)
});
于 2013-09-23T02:45:10.053 回答