1
[Test]
public void Can_Get_All()
{
    var repository = new RavenRepository<Motorcycle>();
    repository.DeleteAll();

    repository.Store(new Motorcycle {Make = "Datsun", YearManufactured = 1972});
    repository.Store(new Motorcycle {Make = "Toyota", YearManufactured = 2002});

    IList<Motorcycle> savedThings = repository.GetAll();

    Assert.IsTrue(savedThings.Count == 2);
}

RavenRepository.GetAll()

public IList<T> GetAll()
{
    using (IDocumentSession session = _collection.OpenSession())
    {
        return session.Query<T>().ToList(); // Throws exception
    }
}

运行此测试会引发异常:

Raven.Abstractions.Exceptions.IndexCompilationException:无法理解查询:变量初始值设定项选择必须具有带有对象创建表达式的 lambda 表达式

为什么?我怎样才能从 RavenDB 中获取所有类型 T 的文档?

4

2 回答 2

2

如果您想要删除所有内容,那么您可以这样做:

public class AllDocumentsById : AbstractIndexCreationTask
{
    public override IndexDefinition CreateIndexDefinition()
    {
        return
            new IndexDefinition
            {
                Name = "AllDocumentsById",
                Map = "from doc in docs 
                      let DocId = doc[\"@metadata\"][\"@id\"] 
                      select new {DocId};"
            };
    }
}

docStore.DatabaseCommands.DeleteByIndex("AllDocumentsById", new IndexQuery());

如果您有要删除的不同索引,那么它也应该可以工作。我们也将这种模式用于一些测试。

于 2013-11-07T00:38:26.740 回答
1

由于 RavenDB 强制执行默认分页,它不会工作。看看这里:http ://ayende.com/blog/161249/ravenbs-querying-streaming-unbounded-results

于 2013-10-29T09:23:50.490 回答