Complex query in nHibernate using DetachedCriteria

Posted by paszczi on Stack Overflow See other posts from Stack Overflow or by paszczi
Published on 2009-02-13T12:14:54Z Indexed on 2010/04/25 5:23 UTC
Read the original article Hit count: 718

Filed under:
|
|

Hello! I'm currently trying to move from hand-crafted hql to queries constructed via DetachedCriteria. I have and HQL:

from GenericObject genericObject 
      left join fetch genericObject.Positions positions
      where (positions.Key.TrackedSourceID, positions.Key.PositionTimestamp) in 
      (select gp.Key.TrackedSourceID, max(gp.Key.PositionTimestamp)
       from GenericPosition gp 
group by gp.Key.TrackedSourceID) 

Now using DetachedCriteria:

var subquery = DetachedCriteria
                .For (typeof (GenericPosition), "gp")
                .SetProjection (Projections.ProjectionList ()
                                    .Add (Projections.Property ("gp.Key.TrackedSourceID"))
                                    .Add (Projections.Max ("gp.Key.PositionTimestamp"))
                                    .Add (Projections.GroupProperty ("gp.Key.TrackedSourceID"))
                );
            var criteriaQuery = DetachedCriteria
                .For (typeof (GenericObject), "genericObject")
                .CreateAlias ("genericObject.Positions", "positions")
                .SetFetchMode ("genericObject.Positions", FetchMode.Eager)
                .Add (Subqueries.In (??, subquery))

I don't know what to type instead of ?? to create expression like (positions.Key.TrackedSourceID, positions.Key.PositionTimestamp)

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate