【问题标题】:RavenDB Query on Datetime with value in collection offsetRavenDB 查询日期时间,集合偏移量中的值
【发布时间】:2012-10-31 19:51:15
【问题描述】:

我正在尝试查询 RavenDB 的日期时间,该日期时间被集合中的条目所抵消。如下所示,我有一个 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 处理它,就像它正在做的那样?

【问题讨论】:

    标签: datetime ravendb


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!你让我指出了正确的方向。我已经用让我工作的代码编辑了我上面的问题。我添加了一个关于最佳实践的问题,我没想到 Raven 只会返回我的文档的 1 个实例,即使我的索引有多个与 where 子句匹配的条目都指向同一个文档。我不确定我是否应该做任何其他事情来提供帮助,或者只是让 Raven 继续处理知道返回每个文档的一个实例。
    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2011-05-18
    • 2020-10-24
    • 2018-02-20
    • 2016-09-09
    相关资源
    最近更新 更多