当只需要日期部分时,NHibernate和Linq无法识别方法调用

时间:2015-11-19 16:27:11

标签: c# linq date nhibernate

我定义了以下类

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.AppointmentDateTime.ToShortDateString()

我认为这是因为它无法确定ToShortDateString的SQL - 所以我应该如何编码这个要求。数据库是SQL Server。

2 个答案:

答案 0 :(得分: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());
}

单元测试通常是查找基本示例的好地方。请参阅此处了解linq的nhibernate单元测试,这些测试特定于日期时间示例:

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

答案 1 :(得分: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函数。