与此问题相关:RavenDB Get By List of IDs? 我所拥有的不起作用:
public class Entity
{
public int CategoryId; //Unique identifier for the category the price applies to.
public int Price; //Changes with time
public DateTime Timestamp; //DateTime.UtcNow
}
public class LastEntityIndex : AbstractIndexCreationTask<Entity, Entity>
{
public LastEntityIndex()
{
Map = prices => prices.Select(a => new { a.CategoryId, a.Price, a.Timestamp });
Reduce = prices => from price in prices
group price by price.CategoryId
into g
let a = g.OrderByDescending(a => a.Timestamp).FirstOrDefault()
select new
{
a.CategoryId,
a.Price,
a.Timestamp
};
}
}
public class LastEntity
{
public int CategoryId;
public DateTime Timestamp;
public Entity LastEntity;
}
public class LastReportIndex : AbstractIndexCreationTask<Entity, LastEntity>
{
public LastReportIndex()
{
Map = reports => reports.Select(a => new { a.CategoryId, a.Timestamp, LastEntity = a });
Reduce = reports => from report in reports
group report by report.CategoryId
into g
let a = g.OrderByDescending(a => a.Timestamp).FirstOrDefault()
select new
{
a.CategoryId,
a.Timestamp,
a.LastEntity
};
}
}
我想创建一个索引来获取每个类别的最新记录。但以上都不起作用。非常感谢任何帮助。为这个新项目离开 sql 并评估 Raven。到目前为止,它似乎非常强大并且可以满足我们的需要,但是从 sql dbs 转变范式很难。
非常感谢你。
PS 使用它来检索记录:
public List<Entity> GetLastEntityForCategoryIds(List<int> categoryIds)
{
using (var session = _documentStore.OpenSession())
{
return session.Query<LastEntity, LastReportIndex>()
.Where(x => x.CategoryId.In(categoryIds))
.Select(a => a.LastEntity)
.ToList();
}
}
显然,“LastEntityIndex”不是我长期使用的东西(它只是为了尝试看看它是否有效),因为真正的实体有更多的字段,只有 3 个,并且将它们全部复制并维护它会非常困难.