I am trying to create a query using QueryOver, which will fetch a collection using the Select or SubSelect mode. The entity in question is Track. I want to load a collection called TrackPrices, and I am doing this in the query:
q = q.Fetch(item => item.TrackPrices).Eager;
However, this creates a left join, which results in a problem for pagination. I would like it to perform a seperate select or subselect. Any idea if it can be done? As far as I know, using the criteria API one would do:
q.DetachedCriteria.SetFetchMode("TrackPrices", FetchMode.Select);
But I want to avoid magic strings in the code, thus I would prefer doing it using the QueryOver API.
From the time I posted this question, I’ve managed to find a workaround / solution which others might find useful as well.
Basically, in such case you must first create another query which distinctly selects the primary keys of the main query, together with pagination. Since the DISTINCT(ID) will return only the results you want, you can use the pagination of SQL without any problems. Then, you re-run the main query, without pagination but using a condition where the ID is in one of the list returned. I created a general method which takes a criteria, looks up the IDs returned and adds them as a condition to the main criteria. Code below:
The methods
NHManager.Instance.GetCurrentSessionFromContext();can be replaced with your own method to retrieve the current session from the session factory.Hope it helps!