值在集合偏移量中的Datetime上的RavenDB查询

值在集合偏移量中的Datetime上的RavenDB查询,datetime,ravendb,Datetime,Ravendb,我试图在Datetime上查询RavenDB,该日期时间被集合中的一个条目抵消。如下所示,我有一个AppointmentReminder对象,其中包含许多AppointmentReminderJobs。我想查询AppointmentMemberJob应在哪里运行的AppointmentMembers 我的模型如下: public class AppointmentReminder { public int Id { get; set; } public string FirstN

我试图在Datetime上查询RavenDB,该日期时间被集合中的一个条目抵消。如下所示,我有一个AppointmentReminder对象,其中包含许多AppointmentReminderJobs。我想查询AppointmentMemberJob应在哪里运行的AppointmentMembers

我的模型如下:

public class AppointmentReminder
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public DateTime AppointmentDateTime { get; set; }
    public ReminderStatus ReminderStatus { get; set; }
    public List<AppointmentReminderJob> AppointmentReminderJobs { get; set; }
 }

public class AppointmentReminderJob
{
    public JobStatus JobStatus { get; set; }
    public int DaysPrior { get; set; }
}
我正试图建立这样一个索引:

public class JobsQueuedListCurrent : AbstractIndexCreationTask<AppointmentReminder, JobsQueuedListCurrent.IndexResult>
{
    public class IndexResult
    {
        public int Id { get; set; }
        public DateTime JobDateTime { get; set; }
    }

    public JobsQueuedListCurrent()
    {


        Map = appointmentreminders => from appointmentreminder in appointmentreminders
                                      from job in appointmentreminder.AppointmentReminderJobs
                                      select new 
                                      { 
                                          Id = appointmentreminder.Id, 
                                          JobDateTime = appointmentreminder.AppointmentDateTime.AddDays(job.DaysPrior)
                                      };
        Store(x => x.Id, FieldStorage.Yes);
        Store(x => x.JobDateTime, FieldStorage.Yes);
    }
}
公共类JobSqueedListCurrent:AbstractIndexCreationTask
{
公共类索引结果
{
公共int Id{get;set;}
公共日期时间作业日期时间{get;set;}
}
公共工作挤入当前
{
Map=任命提醒=>来自任命提醒中的任命提醒
来自AppointmentRememberJobs中的作业。AppointmentRememberJobs
选择新的
{ 
Id=任命提醒。Id,
JobDateTime=appointmentreminder.AppointmentDateTime.AddDays(job.DaysPrior)
};
存储(x=>x.Id,FieldStorage.Yes);
存储(x=>x.JobDateTime,FieldStorage.Yes);
}
}
现在,我使用以下工具查询并获得预期结果:

var jobsqueuedlist = RavenSession.Query<JobsQueuedListCurrent.IndexResult, JobsQueuedListCurrent>()
            .Where(x=>x.JobDateTime >= DateTime.Now)
            .As<AppointmentReminder>()
            .Take(20)
            .ToList();

        return View("List", jobsqueuedlist);
var jobsqueedlist=RavenSession.Query()
.Where(x=>x.JobDateTime>=DateTime.Now)
.As()
.Take(20)
.ToList();
返回视图(“列表”,作业列表);

关于这一点,我的最后一个问题是,我的地图/索引肯定会导致同一文档id(appointmentreminder)的多个条目,但我的结果列表只包含该文档的一个实例。我对它的工作方式很满意,我只是不确定我是应该在代码中执行reduce还是做其他的事情,还是让Raven像现在这样处理它

您不能创建这样的查询。这将要求RavenDB在查询期间执行计算,这是不允许的。 RavenDB只允许查询索引中的数据


您可以做什么?在索引中设置计算,然后查询。

谢谢!你给我指出了正确的方向。我用让我工作的代码编辑了上面的问题。我添加了一个关于最佳实践的问题,我不希望Raven只返回我文档的一个实例,即使我的索引中有多个与where子句匹配的条目都指向同一个文档。我不确定我是否应该做些其他事情来帮助Raven,还是让Raven继续处理,只返回每个文档的一个实例。
public class JobsQueuedListCurrent : AbstractIndexCreationTask<AppointmentReminder, JobsQueuedListCurrent.IndexResult>
{
    public class IndexResult
    {
        public int Id { get; set; }
        public DateTime JobDateTime { get; set; }
    }

    public JobsQueuedListCurrent()
    {


        Map = appointmentreminders => from appointmentreminder in appointmentreminders
                                      from job in appointmentreminder.AppointmentReminderJobs
                                      select new 
                                      { 
                                          Id = appointmentreminder.Id, 
                                          JobDateTime = appointmentreminder.AppointmentDateTime.AddDays(job.DaysPrior)
                                      };
        Store(x => x.Id, FieldStorage.Yes);
        Store(x => x.JobDateTime, FieldStorage.Yes);
    }
}
var jobsqueuedlist = RavenSession.Query<JobsQueuedListCurrent.IndexResult, JobsQueuedListCurrent>()
            .Where(x=>x.JobDateTime >= DateTime.Now)
            .As<AppointmentReminder>()
            .Take(20)
            .ToList();

        return View("List", jobsqueuedlist);