如何在 nHibernate 中使用 datediff sql 函数?

How to use datediff sql function in nHibernate?

我正在开发一个应用程序来测试使用不同框架的查询的执行时间 SQL。我在使用 Fluent nHibernate 编写其中一个查询时遇到了一些问题。查询应该 return 所有年龄超过 50 岁的员工。在某些页面中,我找到了我在下面附加的 DateProjections class。

public static class DateProjections
    {
        private const string DateDiffFormat = "datediff({0}, ?1, ?2)";

        public static IProjection DateDiff(
            string datepart,
            Expression<Func<object>> startDate,
            Expression<Func<object>> endDate)
        {
            // Build the function template based on the date part.
            string functionTemplate = string.Format(DateDiffFormat, datepart);

            return Projections.SqlFunction(
                new SQLFunctionTemplate(NHibernateUtil.Int32, functionTemplate),
                NHibernateUtil.Int32,
                Projections.Property(startDate),
                Projections.Property(endDate));
        }
    }

获取员工的函数如下:

public List<EmployeeAgeViewModel> GetEmployeesOlderThan50()
        {
            Person personAlias = null;
            EmployeeAgeViewModel result = null;

            var temp = _session.QueryOver<Employee>().JoinQueryOver(x => x.Person, () => personAlias).SelectList(
                list => list
                .Select(x => personAlias.FirstName).WithAlias(() => result.FirstName)
                .Select(x => personAlias.LastName).WithAlias(() => result.LastName)
                .Select(x => x.Gender).WithAlias(() => result.Gender)
                .Select(x => x.BirthDate).WithAlias(() => result.BirthDate)
                .Select(x => x.HireDate).WithAlias(() => result.HireDate)
                .Select(DateProjections.DateDiff("yy", () => personAlias.Employee.BirthDate, () => DateTime.Now)).WithAlias(() => result.Age)
                )
                .TransformUsing(Transformers.AliasToBean<EmployeeAgeViewModel>())
                .List<EmployeeAgeViewModel>();

            return temp.ToList();

问题可能是我将 BirthDate 属性 和 DateTime.Now 传递给 DateDiff 函数的方式。在 personAlias 变量中,Employee 属性 为 null - 也许我应该之前以某种方式分配它 - 任何帮助将不胜感激。

您需要使用 employeeAlias 而不是 personAlias.Employee.BirthDate

经过必要更改的代码如下所示:

Person personAlias = null;
Employee employeeAlias = null;
EmployeeAgeViewModel result = null;

var temp = _session.QueryOver<Employee>(() => employeeAlias)
    .JoinQueryOver(x => x.Person, () => personAlias)
    .SelectList(
        list => list
        .Select(x => personAlias.FirstName).WithAlias(() => result.FirstName)
        .Select(x => personAlias.LastName).WithAlias(() => result.LastName)
        .Select(x => x.Gender).WithAlias(() => result.Gender)
        .Select(x => x.BirthDate).WithAlias(() => result.BirthDate)
        .Select(x => x.HireDate).WithAlias(() => result.HireDate)
        .Select(DateProjections.DateDiff("yy", () => employeeAlias.BirthDate, () => DateTime.Now))
            .WithAlias(() => result.Age)
        )
    .TransformUsing(Transformers.AliasToBean<EmployeeAgeViewModel>())
    .List<EmployeeAgeViewModel>();

return temp.ToList();