I am try to construct this query in nHibernate:
SELECT max(split_part(person.Name,'-',2))
FROM data.person
How can I make this using projections ?
I have this at the moment :
session.QueryOver<PersonEntity>()
.Select(Projections.Max<PersonEntity>(x=>Projections.SqlFunction("split_part", NHibernateUtil.String, Projections.Property<PersonEntity>(p=>p.Name), Projections.Constant("-"), Projections.Constant(2))))
.SingleOrDefault<int>()
But I can make it work in nHibernate.
You're close:
You could also clean this up a bit by registering the function in your own dialect (I'm assuming you're using PostgreSQL:
And then use that dialect instead inside of your configuration file.
Then, you could add your own custom method that calls the
split_part
method:Then, your query would end up looking a little cleaner: