Nhibernate Criteria: 'select max(id)...'

AwkwardCoder picture AwkwardCoder · Aug 14, 2009 · Viewed 14.1k times · Source

Can I use a Criteria to execute a t-sql command to select the max value for a column in a table?

'select @cus_id = max(id) + 1 from customers'

Ta

Ollie

Answer

ChssPly76 picture ChssPly76 · Aug 14, 2009

Use Projection:

session.CreateCriteria(typeof(Customer))
  .SetProjection( Projections.Max("Id") )
  . UniqueResult();