6

我正在尝试在 Datetime 上查询 RavenDB,该 Datetime 被集合中的条目所抵消。如下所示,我有一个 AppointmentReminder 对象,其中包含许多 AppointmentReminderJobs。我想查询 AppointmentReminders 应在哪里运行 AppointmentReminderJob。

我的模型如下:

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; }
}

我的控制器并尝试检索具有当前要运行的作业的 AppointmentReminders 列表(我知道此 Where 子句不完整,但我试图简化它但没有运气):

public ActionResult GetJobsQueuedListCurrent()
    {
        var jobsqueuedlist = RavenSession.Query<AppointmentReminder>()
            .Where(appointmentreminder => appointmentreminder.AppointmentReminderJobs.Any(x => appointmentreminder.AppointmentDateTime < DateTime.Now.AddDays(x.DaysPrior)))
            .OrderBy(appointmentreminder => appointmentreminder.AppointmentDateTime)
            .Take(20)
            .ToList();

        return View("List", jobsqueuedlist);

    }

调用上述代码会产生以下响应:

variable 'x' of type 'ProjectName.Models.AppointmentReminderJob' referenced from scope '', but it is not defined

我正在尝试像这样设置索引:

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);

我关于此的最后一个问题是,我的地图/索引肯定会导致同一文档 ID 的多个条目(约会提醒),但我的结果列表仅包含文档的 1 个实例。我对它的工作方式感到满意,我只是不确定我是否应该在我的代码中执行减少或做其他事情,或者只是让 Raven 处理它,就像它正在做的那样?

4

1 回答 1

5

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

可以做什么它在索引中设置计算,然后在那个上查询。

于 2012-11-01T08:00:34.743 回答