C# 查询子文档Ravendb
截图: 您只能在RavenDB中查询根文档。 现在,您可以查询子对象中包含特定值的所有根文档(不是子文档,没有这样的内容),但您仍然需要根文档 例如,查询如下所示:给我所有包含关键字“nice”的记录:C# 查询子文档Ravendb,c#,linq,ravendb,C#,Linq,Ravendb,截图: 您只能在RavenDB中查询根文档。 现在,您可以查询子对象中包含特定值的所有根文档(不是子文档,没有这样的内容),但您仍然需要根文档 例如,查询如下所示:给我所有包含关键字“nice”的记录: session.Query().Where(x=>x.Keywords.Contains(“Nice”)).ToList(); 你可以问,从所有的记录中,给我他们的关键词: session.Query<Record>().Where(x=>x.Keywords.Contain
session.Query().Where(x=>x.Keywords.Contains(“Nice”)).ToList();
你可以问,从所有的记录中,给我他们的关键词:
session.Query<Record>().Where(x=>x.Keywords.Contains("Nice")).ToList();
session.Query().Select(x=>x.Keywords.ToList();
但是你总是从根文档开始。如果你想直接查询关键字,你可以写一个索引
session.Query<Record>().Select(x=> x.Keywords).ToList();
那就叫它:
documentStore.Initialize();
documentStore.ExecuteIndex(new KeywordsIndex());
使用(var session=documentStore.OpenSession())
{
session.Query().Select(r=>r.Keyword.ToList();
}
通过这种方式,您可以实际查询关键字、排序或筛选等。
请参阅此处有关静态索引的更多信息:
您可能可以直接从索引返回关键字,但不确定,但如果可以的话,这对您来说可能很好。当我这样做时,visual studio不允许我在linq表达式中指定子文档?有关更多详细信息,请参阅问题编辑编译器应从类-
公共类关键字index:AbstractIndexCreationTask
中获取类型。因为Map
是一个Func
,编译器可以从中推断。确保在构造函数中执行此操作。
session.Query<Record>().Where(x=>x.Keywords.Contains("Nice")).ToList();
session.Query<Record>().Select(x=> x.Keywords).ToList();
public class KeywordsIndex : AbstractIndexCreationTask<Record, KeywordsIndex.Result>
{
public KeywordsIndex()
{
Map = records => from record in records
from keyword in record.Keywords
select new
{
Keyword = keyword
}
}
public class Result
{
public Keyword Keyword { get; set; }
}
}
documentStore.Initialize();
documentStore.ExecuteIndex(new KeywordsIndex());
using (var session = documentStore.OpenSession())
{
session.Query<KeywordsIndex.Result, KeywordsIndex>().Select(r => r.Keyword).ToList();
}