gpt4 book ai didi

NHibernate QueryOver 关联不包含项目

转载 作者:行者123 更新时间:2023-12-01 11:04:29 28 4
gpt4 key购买 nike

谁能帮我把 LINQ 表达式翻译成 Nhibernate QueryOver

from m in messages
where !m.Recipients.Any(rcpt => rcpt.IsDeleted && rcpt.User = user)

我试过了

var qry = Session.QueryOver<UserMessage>();
qry.Where(m => m.Recipients.Any(r => !r.IsDeleted && r.User == user));

但是得到了

System.Exception:无法识别的方法调用:System.Linq.Enumerable:Boolean Any[TSource](System.Collections.Generic.IEnumerable1[TSource], System.Func2[TSource,System . bool 值]

最佳答案

!m.Recipients.Any(...) 转换为“不存在”子查询。您将需要几个别名来将子查询与主查询相关联,并且子查询需要有一个投影才能让 NHibernate 满意。

尝试这样的事情:

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();

关于NHibernate QueryOver 关联不包含项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7370205/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com