Using a subquery for a column with QueryOver

左心房为你撑大大i 提交于 2019-12-23 17:33:48

问题


I'm trying to get something similar to the SQL below via QueryOver:

SELECT
    docs.*,
    (SELECT TOP 1 eventDate from events WHERE id=docs.id 
    AND type=4 ORDER BY eventDate DESC) as eventDate
FROM documents as docs
WHERE doc.accountId = ...

I've got close with a projection, however I'm not sure how to get the entire documents table back. Documents has a one-to-many relationship with Events, I don't want to outer join as it will bring multiple results, and an inner join may not bring back a row:

var query = QueryOver<Document>
    .Where(d => d.Account == account)
    .SelectList(list => list
        .Select(d => d)
        .Select(d => d.Events.OrderByDescending(e => e.EventDate).FirstOrDefault(e => e.Type == 4))
    )
    .List<object[]>()
    .Select(d => return new DocumentSummary(d[0],d[1]) etc.);

Is there an easier way of performing subqueries for columns? I'm reluctant to replace this with the property performing a query in its get.


回答1:


After some research it looks like HQL (which QueryOver is converted into) does not support TOP inside subqueries.

My solution: create a view which includes the computed properties, and then mark these properties in the mappings files as insert="false" and update="false"



来源:https://stackoverflow.com/questions/7133983/using-a-subquery-for-a-column-with-queryover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!