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

本文关键字:调用 Linq 识别 方法 NHibernate 使用 日期部 分时 | 更新日期: 2023-09-27 18:22:35

我有以下类定义的

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。

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

我想您正在寻找这样的东西(这是使用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

查询分为两部分,在后半部分对患者标识符的查询结果进行日期检查。所以结尾的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函数的等价物。