RavenDB使用集合偏移量中的值查询日期时间

时间:2012-10-31 19:51:15

标签: datetime ravendb

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

我的模型如下:

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

我的控制器并尝试检索具有当前作业的AppointmentReminder列表(我知道这个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);

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

1 个答案:

答案 0 :(得分:4)

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

可以在索引中设置计算,然后在上查询

相关问题