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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在nHibernate中使用datediff sql函数?相关的知识,希望对你有一定的参考价值。

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

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 - 也许我应该以某种方式分配它 - 任何帮助将不胜感激。

答案

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

具有必要更改的代码如下所示:

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();

以上是关于如何在nHibernate中使用datediff sql函数?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 NHibernate 在 CompositeId 映射中设置列

SQL如何使用datediff排除数据并且不存在于同一个表中

如何使用 DISTINCT 在 NHibernate SQL 查询中进行分页

如何在 nHibernate 中添加 NOLOCK?

如何在VB 中使用datediff计算某开始日期到结束日期之间的月数,并均分到每年

如何在 ORM (NHibernate) 中模拟这种情况?