对同一表/对象的 Linq 条件查询
您好,我在使用条件查询时遇到问题。我希望所有项目的 project.Parent 要么为空,要么如果它有一个父项,那么它不应该被作废或关闭。
我的示例不会带回任何project.Parent == null 的项目。
我们正在使用 linq-to-nhibernate
var projects = (from project in this.Session.Query<Project>()
where project.IsClosed == false
&& project.IsVoided == false
&& (project.Parent == null
|| (project.Parent.IsVoided == false
&& project.Parent.IsClosed == false))
select project).ToList();
Hi I'm having a problem with getting a conditional query to work. I want all projects where project.Parent either is null or if it has a parent then it shouldn't be voided or closed.
My example will NOT bring back any projects where project.Parent == null.
We are using linq-to-nhibernate
var projects = (from project in this.Session.Query<Project>()
where project.IsClosed == false
&& project.IsVoided == false
&& (project.Parent == null
|| (project.Parent.IsVoided == false
&& project.Parent.IsClosed == false))
select project).ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该查询将不起作用,因为内部联接是为 Parent 属性生成的。
最简单的解决方法是执行两个查询并在客户端连接它们:
That query won't work because inner joins are generated for the Parent property.
The easiest workaround is doing two queries and joining them client-side:
我建议获取所有项目并检查应该为空的项目会发生什么情况。没有任何示例数据等,我必须猜测是什么导致了问题。我想说项目父项目是用一些空状态初始化的。
I'd suggest to fetch all project and to check what happens to projects that should be null. Without having any example data etc. I have to guess what is causing the problem. I'd say the project parents are initialized with some empty state.
不需要在客户端进行连接:
Doing the join client-side is not required: