2015-11-19 44 views
0

我有以下類中定義無法識別的方法調用時只有日期部分需要

public class IRM_TEMP_Appointment_archive 
{ 
    public virtual String VisitNumber { get; set; } 
    public virtual String WardCode { get; set; } 
    public virtual String ClinicCode { get; set; } 
    public virtual String DoctorCode { get; set; } 
    public virtual String AppointmentStatusCode { get; set; } 
    public virtual String PatientNumber { get; set; } 
    public virtual String EpisodeNumber { get; set; } 
    public virtual String ConsultantCode { get; set; } 
    public virtual String ReferrerCode { get; set; } 
    public virtual String MainSpecialtyCode { get; set; } 
    public virtual String MessageType { get; set; } 
    public virtual String EpisodeStatusCode { get; set; } 
    public virtual String ParentEpisodeType { get; set; } 
    public virtual String AdmissionType { get; set; } 
    public virtual String AdmitReason { get; set; } 
    public virtual String BuildingCode { get; set; } 
    public virtual String BedNumber { get; set; } 
    public virtual String CategoryCode { get; set; } 
    public virtual String ConsultantName { get; set; } 
    public virtual String DischargeDestination { get; set; } 
    public virtual String DischargeMethod { get; set; } 
    public virtual String FacilityCode { get; set; } 
    public virtual String ReferralReason { get; set; } 
    public virtual String ReferrerAssigningAuthority { get; set; } 
    public virtual String ReferrerName { get; set; } 
    public virtual String currentStatus { get; set; } 
    public virtual String AppointmentTypeText { get; set; } 
    public virtual String TCILocation { get; set; } 

    public virtual Boolean analysed { get; set; } 

    public virtual DateTime AdmitDateTime { get; set; } 
    public virtual DateTime StopDateTime { get; set; } 
    public virtual DateTime ScheduledDateTime { get; set; } 
    public virtual DateTime AppointmentDateTime { get; set; } 
    public virtual DateTime DischargeDateTime { get; set; } 
    public virtual DateTime timeOfInsert { get; set; } 
    public virtual DateTime timeOfAnalyse { get; set; } 
    public virtual DateTime TCIDate { get; set; } 

    public virtual int TempAppointmentKey { get; set; } 

我需要找到任何預約的患者,對一個給定的日期,爲了這個,我只是需要提取物日期

  using (ISessionFactory factory = CreateSessionFactory()) 
      { 
       using (var session = factory.OpenSession()) 
       { 
        var query = session.QueryOver<IRM_TEMP_Appointment_archive>() 
         .Where(a => (a.PatientNumber == MRN) && (a.AppointmentDateTime.ToShortDateString() == DateOfAppointment.ToShortDateString())) 
         .List() 
         .ToList<IRM_TEMP_Appointment_archive>(); 


        if (query != null) 
        { 
         results = query; 
        } 
       } 
      } 

然而,當這個代碼執行,我得到一個錯誤的epression a.AppointmentDateTim

無法識別的方法調用e.ToShortDateString()

我認爲這是因爲它無法確定ToShortDateString的SQL - 所以我該如何編碼這個要求。數據庫是SQL Server。

回答

0

我假定你正在尋找這樣的事情(這是使用LINQ提供程序,而不是QueryOver):

[Test] 
public void CanQueryByDate() 
{ 
    var x = (from o in db.Orders 
     where o.OrderDate.Value.Date == new DateTime(1998, 02, 26) 
     select o).ToList(); 

    Assert.AreEqual(6, x.Count()); 
} 

單元測試通常是找到基本示例的好地方。看到這裏NHibernate的單元測試LINQ所特有的日期時間的例子:

https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate.Test/Linq/DateTimeTests.cs

+0

謝謝。我最終受騙 - 它不會贏得任何獎勵,但效果確實很好。我會把結果放在答案中,而不是把它壓在這裏。 –

0

查詢被一分爲二,在下半年完成上詢問患者的結果的日期檢查標識符。所以最後的if語句變成了;

if (query != null) 
{ 
    results = (from a in query where a.AppointmentDateTime.Date == DateOfAppointment.Date select a).ToList<IRM_TEMP_Appointment_archive>(); 
} 

因爲後面的查詢沒有被轉換成T-SQL,所以不用擔心它們不是Date函數的等價物。