2012-10-31 38 views
6

我想在集合中的某個條目偏移的日期時間查詢RavenDB。如下所示,我有一個AppointmentReminder對象,其中包含許多AppointmentReminderJobs。我想查詢AppointmentReminders AppointmentReminderJob將在哪裏運行。RavenDB在日期時間查詢集合中的值偏移

我的型號如下:

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

我的控制器和嘗試檢索具有當前工作運行(我知道這Where子句是不完整的AppointmentReminders的名單,但我已經盡力沒有運氣簡化它):

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(appointmentreminder)的多個條目,但是我的結果列表僅包含該文檔的一個實例。我很滿意這種工作方式,我只是不確定我是否應該在代碼中執行reduce或做其他事情,或者讓Raven像處理它一樣處理它?

回答

4

您不能創建這樣的查詢。這需要RavenDB在查詢過程中執行計算,這是不允許的。 RavenDB只允許查詢索引中的數據。

可以它是否在索引中設置計算,然後在上查詢那個

+0

謝謝!你讓我指出了正確的方向。我已經編輯我的問題上面的代碼,讓我工作。我添加了一個關於最佳實踐的問題,我並不期待Raven只會返回我的文檔的一個實例,即使我的索引有多個匹配where子句的條目都指向同一個文檔。我不確定我是否應該做其他任何事情來幫助或者讓Raven繼續處理知識,然後返回每個文檔的一個實例。 – mcbowes