NHibernate QueryOver association does not contain item

為{幸葍}努か 提交于 2019-12-04 12:27:39

!m.Recipients.Any(...) translates to a "not exists" sub-query. You will need a couple of aliases to correlate the sub-query with the main query, and the sub-query will need to have a projection to make NHibernate happy.

Try something like this:

UserMessage messageAlias = null;
UserMessage recipientMessageAlias = null;

var subquery = QueryOver.Of<MessageRecipient>()
    .JoinAlias(x => x.Message, () => recipientMessageAlias)
    .Where(x => x.IsDeleted == true) // your criteria
    .Where(x => x.User.Id == userId)
    .Where(() => recipientMessageAlias.Id == messageAlias.Id) // correlated subquery
    .Select(x => x.Id); // projection

var query = session.QueryOver(() => messageAlias)
    .Where(Subqueries.WhereNotExists(subquery));

return query.List();

I managed to it this way:

UserMessage messageAlias = null;

var qry = Session.QueryOver<UserMessage>(() => messageAlias);

UserMessageRecipient recipientAlias = null;

var deletedbyUser = QueryOver.Of(() => recipientAlias)
  .Select(x => x.Id)
  .Where( () => recipientAlias.Message.Id == messageAlias.Id
    && (recipientAlias.Recipient == query.User && recipientAlias.IsDeleted))
                .DetachedCriteria;
qry.Where(Subqueries.NotExists(deletedbyUser));

Try to use the Linq version with session.Query<> instead of QueryOver

var qry = Session.Query<UserMessage>();
qry.Where(m => m.Recipients.Any(r => !r.IsDeleted && r.User == user));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!