RavenDB在Datetime上的查询,其值在收集偏移量中 [英] RavenDB Query on Datetime with value in collection offset
问题描述
我正在尝试在Datetime中查询RavenDB,该日期时间被集合中的条目所抵消。如下所示,我有一个AppointmentReminder对象,其中包含许多AppointmentReminderJobs。我想查询AppointmentReminderJob应运行的AppointmentReminders。
I am trying to query RavenDB on a Datetime which is being offset by a entry in a collection. As shown below, I have an AppointmentReminder object which contains many AppointmentReminderJobs. I'd like to query for AppointmentReminders where the AppointmentReminderJob is due to run.
我的模型如下:
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子句不完整,但我试图简化它,没有运气):
My controller and attempt to retrieve a list of AppointmentReminders which have current jobs to run (I know this Where clause isn't complete, but I've tried to simplify it with no luck):
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);
}
调用以上产生一个响应:
Calling the above yields a response of:
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);
}
}
现在,我正在查询并获得预期的结果:
Now, I'm querying and getting expected results using:
var jobsqueuedlist = RavenSession.Query<JobsQueuedListCurrent.IndexResult, JobsQueuedListCurrent>()
.Where(x=>x.JobDateTime >= DateTime.Now)
.As<AppointmentReminder>()
.Take(20)
.ToList();
return View("List", jobsqueuedlist);
我的最后一个问题是,我的地图/索引肯定会导致相同的多个条目document id(约会),但是我的结果列表只包含1个文档的实例。我很高兴与工作方式,我只是不知道如果我应该在我的代码中执行减少或做别的,或者只是让Raven处理它,就像它在做什么?
My last question regarding this would be, my map/index can definitely result in multiple entries of the same document id (appointmentreminder), but my resulting list contains only 1 instance of the document. I'm happy with the way that works, I'm just not sure if I should be performing a reduce or doing something else in my code or just let Raven handle it like it seems like it is doing?
推荐答案
您不能创建这样的查询。这将需要RavenDB在查询期间执行计算,这是不允许的。
RavenDB仅允许对索引中的数据进行查询。
You cannot create such a query. This would require RavenDB to perform computation during query, and that is not allowed. RavenDB only allows queries on the data in the index.
您可以在索引中设置计算,然后查询 。
这篇关于RavenDB在Datetime上的查询,其值在收集偏移量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!