Search code examples
nhibernatenhibernate-3

NHibernate compare concatenated properties


How would you do this

Select *
from Personnel p
where p.LastName + ', ' + p.FirstName + ' ' + p.MiddleInitial LIKE @Employee + '%'

using NHibernate (3.0)? So far, I've tried

personnel.QueryOver<Personnel>()
    .WhereRestrictionOn( x => x.LastName + ', ' + x.FirstName + ' ' + x.MiddleInitial)
    .IsLike(employeeName, MatchMode.Start)

to no avail.


Solution

  • If you mapped those three columns as a single property using Formula, it will work:

    public class EmployeeMap : ClassMap<Employee>
    {
        public EmployeeMap()
        {
            Table("Employees");
    
            Id(x => x.Id);
            Map(x => x.Name)
              .Formula("UPPER(LTRIM(RTRIM(FirstName + ' ' + MiddleName + ' ' + LastName)))");
    
            // etc.
        }
    }
    

    That's an example using SQL Server, in Oracle, you would switch the ' for | and ditch LTRIM and RTRIM.