Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Distinct操作创建索引。乌鸦_C#_Indexing_Ravendb - Fatal编程技术网

C# 如何使用Distinct操作创建索引。乌鸦

C# 如何使用Distinct操作创建索引。乌鸦,c#,indexing,ravendb,C#,Indexing,Ravendb,我需要的是检索存储在产品中的类别列表: Products.Select(x=>x.Category).Distinct().OrderBy(x=>x) 当调用这个RavenDB时,它说我应该使用索引,因为在查询过程中不允许计算 我读了一些关于索引的书,但仍然不明白我是如何创建索引的 到目前为止,我尝试的是: 初始化 public class DataAccessModule : NinjectModule { public override void Load() { B

我需要的是检索存储在产品中的类别列表:

Products.Select(x=>x.Category).Distinct().OrderBy(x=>x)

当调用这个
RavenDB
时,它说我应该使用索引,因为在查询过程中不允许计算

我读了一些关于索引的书,但仍然不明白我是如何创建索引的

到目前为止,我尝试的是:

初始化

public class DataAccessModule : NinjectModule {
    public override void Load() {
        Bind<IDocumentStore>().ToMethod(
            context => {
               var documentStore = new EmbeddableDocumentStore {
                   DataDirectory = @"~/App_Data/database",
                   UseEmbeddedHttpServer = true,
                   DefaultDatabase = "SampleStore"
               };
               var store = documentStore.Initialize();
               IndexCreation.CreateIndexes(typeof(CategoriesIndex).Assembly, store);
               return store;
           }
       ).InSingletonScope();

        Bind<IDocumentSession>().ToMethod(context => 
            context.Kernel.Get<IDocumentStore>().OpenSession()
        ).InRequestScope();
    }
}
public class CategoriesIndex : AbstractIndexCreationTask<Product> {
    public CategoriesIndex() {
        Map = ct => ct.Select(x => x.Categories).Distinct().OrderBy(x => x);
    }
}
公共类DataAccessModule:NinjectModule{
公共覆盖无效负载(){
Bind().ToMethod(
上下文=>{
var documentStore=新的EmbeddedBleDocumentStore{
DataDirectory=@“~/App\u数据/数据库”,
UseEmbeddedHttpServer=true,
DefaultDatabase=“SampleStore”
};
var store=documentStore.Initialize();
CreateIndexes(typeof(CategoriesIndex).Assembly,store);
退货店;
}
).InSingletonScope();
Bind().ToMethod(上下文=>
context.Kernel.Get().OpenSession()
).InRequestScope();
}
}
索引定义

public class DataAccessModule : NinjectModule {
    public override void Load() {
        Bind<IDocumentStore>().ToMethod(
            context => {
               var documentStore = new EmbeddableDocumentStore {
                   DataDirectory = @"~/App_Data/database",
                   UseEmbeddedHttpServer = true,
                   DefaultDatabase = "SampleStore"
               };
               var store = documentStore.Initialize();
               IndexCreation.CreateIndexes(typeof(CategoriesIndex).Assembly, store);
               return store;
           }
       ).InSingletonScope();

        Bind<IDocumentSession>().ToMethod(context => 
            context.Kernel.Get<IDocumentStore>().OpenSession()
        ).InRequestScope();
    }
}
public class CategoriesIndex : AbstractIndexCreationTask<Product> {
    public CategoriesIndex() {
        Map = ct => ct.Select(x => x.Categories).Distinct().OrderBy(x => x);
    }
}
公共类分类索引:AbstractIndexCreationTask{
公共分类索引(){
Map=ct=>ct.Select(x=>x.Categories).Distinct().OrderBy(x=>x);
}
}
但这不起作用。 为什么我要用正确的方式来定义它

谢谢

您可以使用以下方法执行此操作:

var categories = Session.Query<Product>()
                   .Select(x => x.Category).Distinct().ToList().OrderBy(x=>x);
var categories=Session.Query()
.Select(x=>x.Category).Distinct().ToList().OrderBy(x=>x);

这将为您提供您想要的。

那么是否不需要创建索引?