If you prefer INNER SELECT , NHiberante has the solution for you. It is called DetachedCriteria . (try checking here for a similar example)
So, first create an internal selection:
var sub = DetachedCriteria .For<UserTask>() // WHERE .Add(Restrictions.In("UserId", new int[] { 1, 2 })) // example of filtering 'user_id' .Add(Restrictions.Eq("solved", true)) // 'solved' .SetProjection(Projections.Property("TaskId")); // TaskId the SELECT clause
(I'm not sure about your model and name, e.g. task_id vs TaskId ... but the intent should be clear)
With the help of DetachedCritera we can do a lot, we can join other reference objects / tables, filter them ... as with the standard QueryOver. The only difference is that we have to return Projection (SELECT TaskId), which will be used as a filter in another query:
var criteria = session.CreateCriteria<Task>(); criteria . Where(... // your filters applied on Task .Add(Subqueries.PropertyIn("ID", sub)); // Task.ID in (select...
var result = criteria.List ();
Note. This solution will not only work;), but basically, it is ready for paging and sorting. Therefore, even in cases where the internal selection returns more “similar” results (the same identifiers), the top selection will return each task only once ...
For more information: 15.8. Separate queries and subqueries
source share