I have SQL query like this:
select * from dbo.table1 where Id in
(
select max(id) as id from dbo.table1 group by prop1, prop2, prop3
)
I want to create NHibernate query which is be able to do this for me. I tried to use QueryOver
but it doesn't work. Do you have any suggestions how to do it?
NHibernate supports even this kind of queries. Please, see more in documentation: 15.8. Detached queries and subqueries. We just have to split the query (as in your SQL snippet) into two parts:
Let's assume, that the
dbo.table1
in the Questin is mapped intoMyEntity
. To create inner select, let's use theDetachedCriteria
EDIT (extended with the Group by,
SqlGroupProjection
)There is an extract of the
SqlGroupProjection
method:Note: I've provided even the last two paramters, but in this case they could be empty:
new string[], new IType[] {}
. These are used only for Transformation (materialization from data into entity). And this is not the case, we are just building inner select...Also related could be 15.7. Projections, aggregation and grouping