对同一表/对象的 Linq 条件查询

发布于 2024-10-14 22:18:10 字数 577 浏览 2 评论 0原文

您好,我在使用条件查询时遇到问题。我希望所有项目的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书间行客 2024-10-21 22:18:10

该查询将不起作用,因为内部联接是为 Parent 属性生成的。

最简单的解决方法是执行两个查询并在客户端连接它们:

var projects = (from project in this.Session.Query<Project>()
                where project.IsClosed == false
                   && project.IsVoided == false
                   && project.Parent == null 
                select project)
               .AsEnumerable()
               .Concat(
               (from project in this.Session.Query<Project>()
                where project.IsClosed == false
                   && project.IsVoided == false
                   && project.Parent.IsVoided == false
                   && project.Parent.IsClosed == false
                select project))
               .ToList();

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:

var projects = (from project in this.Session.Query<Project>()
                where project.IsClosed == false
                   && project.IsVoided == false
                   && project.Parent == null 
                select project)
               .AsEnumerable()
               .Concat(
               (from project in this.Session.Query<Project>()
                where project.IsClosed == false
                   && project.IsVoided == false
                   && project.Parent.IsVoided == false
                   && project.Parent.IsClosed == false
                select project))
               .ToList();
断肠人 2024-10-21 22:18:10

我建议获取所有项目并检查应该为空的项目会发生什么情况。没有任何示例数据等,我必须猜测是什么导致了问题。我想说项目父项目是用一些空状态初始化的。

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.

心奴独伤 2024-10-21 22:18:10

不需要在客户端进行连接:

var projects = (from project in this.Session.Query<Project>()
            where project.Parent == null || (project.IsClosed == false
               && project.IsVoided == false)
               && (project.Parent == null 
               || (project.Parent.IsVoided == false
                   && project.Parent.IsClosed == false))
            select project).ToList();

Doing the join client-side is not required:

var projects = (from project in this.Session.Query<Project>()
            where project.Parent == null || (project.IsClosed == false
               && project.IsVoided == false)
               && (project.Parent == null 
               || (project.Parent.IsVoided == false
                   && project.Parent.IsClosed == false))
            select project).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文