【发布时间】:2013-11-05 13:46:27
【问题描述】:
与本题相关: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 个并将它们全部复制并维护它会非常努力。
【问题讨论】:
-
什么不起作用?编译错误?错误的数据回来了?你能告诉我们什么数据回来了吗?您如何检查数据是否合法?索引是否仍然过时?
-
@Pure.Krome 索引不是陈旧的,(使用 WaitForNonStaleResultsAsOfNow 在单元测试中运行)。数据返回错误或所有默认值。
-
@RomanStefanidi 你能发布一个完整的单元测试的要点吗? This guide may help.