如何在 linq to sql 中选择全部

发布于 2024-10-19 05:22:49 字数 440 浏览 1 评论 0原文

在我的 LINQ 查询中,如果值为 null,我尝试选择全部:

var filteredLesson = (from l in storeDB.lessons
                      where l.statusID == SUBMITTED ||
                            l.statusID == APPROVED &&
                            l.projectID == (l.projectID.HasValue 
                                ? lesson.projectID : /*select all*/  )
                      select l).ToList();

有办法做到这一点吗?

In my LINQ query, I am trying to select all if a value is null:

var filteredLesson = (from l in storeDB.lessons
                      where l.statusID == SUBMITTED ||
                            l.statusID == APPROVED &&
                            l.projectID == (l.projectID.HasValue 
                                ? lesson.projectID : /*select all*/  )
                      select l).ToList();

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

辞取 2024-10-26 05:22:49

如果值为 null,您只想将其与自身进行比较。这样,如果值为 null,您的行将包含在结果中。

var filteredLesson = (from l in storeDB.lessons
                      where l.statusID == SUBMITTED ||
                      l.statusID == APPROVED &&
                      l.projectID == (l.projectID.HasValue 
                                   ? lesson.projectID : l.projectID )
                      select l).ToList();

You just want to compare it to itself if the value is null. That way your row will be included in the results if the value is null.

var filteredLesson = (from l in storeDB.lessons
                      where l.statusID == SUBMITTED ||
                      l.statusID == APPROVED &&
                      l.projectID == (l.projectID.HasValue 
                                   ? lesson.projectID : l.projectID )
                      select l).ToList();
梦幻的心爱 2024-10-26 05:22:49

也许是这样的?

var filteredLesson = (from l in storeDB.lessons
                      where
                          (l.statusID == SUBMITTED || l.statusID == APPROVED)
                          &&
                          (!l.projectID.HasValue || l.projectID == lesson.projectID)
                      select l
                     ).ToList();

Something like this perhaps?

var filteredLesson = (from l in storeDB.lessons
                      where
                          (l.statusID == SUBMITTED || l.statusID == APPROVED)
                          &&
                          (!l.projectID.HasValue || l.projectID == lesson.projectID)
                      select l
                     ).ToList();
ζ澈沫 2024-10-26 05:22:49

在不知道您的对象模型的情况下,很难完全确定这一点,但我想您应该寻找类似以下的内容:

var filteredLesson = storeDB.lessons
                            .Where(l => (l.statusID == SUBMITTED
                                       || l.statusID == APPROVED)
                                       && !l.projectID.HasValue)
                            .ToList();

Without knowing your object model it's hard to be totally sure about this, but I imagine something like the following should be what you're looking for:

var filteredLesson = storeDB.lessons
                            .Where(l => (l.statusID == SUBMITTED
                                       || l.statusID == APPROVED)
                                       && !l.projectID.HasValue)
                            .ToList();
厌倦 2024-10-26 05:22:49

以下是使用 xUnit.Net 测试编写的上述许多答案。请注意,原作者在他选择的答案中的评论(这是一个错误的解决方案)给出了他期望的结果。我清理了他的解决方案以删除重复的比较。

示例值位于底部。

    public class SelectAllInLinq2SqlTests
    {
        [Fact]
        public void Unsuccessful()
        {
            var filteredLesson =
                (from l in Lessons
                 where l.statusID == SUBMITTED ||
                       l.statusID == APPROVED &&
                       l.projectID == (l.projectID.HasValue ? projectOne : l.projectID)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);

            // this returned 5 items!
            // good - filtered the null project IDs and statusID == SUBMITTED
            // good - filtered the null project IDs and statusID == APPROVED
            // good - filtered the projectID == projectOne and statusID == SUBMITTED
            // good - filtered the projectID == projectOne and statusID == APPROVED
            // FAIL - filtered the projectID == projectTwo !!!

            // two failures first selecting all records containing a null projectID and any status (we wanted to filter on SUBMITTED or APPROVED)
            // the failure is due to operator precedence the 'l.statusID == SUBMITTED ||' is short-circuiting the rest of the comparison
        }

        [Fact]
        public void Unsuccessful2()
        {
            var filteredLesson =
                (from l in Lessons
                 where !l.projectID.HasValue ||
                       (l.statusID == SUBMITTED || l.statusID == APPROVED && l.projectID == projectOne)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);

            // this returned 6 items!
            // good - filtered the null project IDs and statusID == SUBMITTED
            // good - filtered the null project IDs and statusID == APPROVED
            // FAIL - filtered the null project IDs and statusID == OTHER!
            // good - filtered the projectID == projectOne and statusID == SUBMITTED
            // good - filtered the projectID == projectOne and statusID == APPROVED
            // FAIL - filtered the projectID == projectTwo !!!

            // Two failures:
            //   first selecting all records containing a null projectID and any status (we wanted to filter on SUBMITTED or APPROVED)
            //   second failure is due to operator precedence the 'l.statusID == SUBMITTED ||' is short-circuiting the rest of the comparison
        }

        [Fact]
        public void Unsuccessful2NotQuiteFixed()
        {
            var filteredLesson =
                (from l in Lessons
                 where !l.projectID.HasValue
                       || ((l.statusID == SUBMITTED || l.statusID == APPROVED)
                           && l.projectID == projectOne)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);
            // this returned 5 items!
            // good - filtered the null project IDs and statusID == SUBMITTED
            // good - filtered the null project IDs and statusID == APPROVED
            // FAIL - filtered the null project IDs and statusID == OTHER!
            // good - filtered the projectID == projectOne and statusID == SUBMITTED with a SUBMITTED status
            // good - filtered the projectID == projectOne and statusID == APPROVED with a APPROVED status

            // failures selecting all records containing a null projectID and any status (we wanted to filter on SUBMITTED or APPROVED)
        }

        [Fact]
        public void Sucessful()
        {
            // Questioner's provided solution...

            var filteredLesson =
                (from l in Lessons
                 where !l.projectID.HasValue && (l.statusID == SUBMITTED || l.statusID == APPROVED)
                       || l.projectID == projectOne && (l.statusID == SUBMITTED || l.statusID == APPROVED)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);
        }

        [Fact]
        public void SucessfulRefined()
        {
            // cleaned up without the duplicate status comparisons. Note the parens are necessary.

            var filteredLesson =
                (from l in Lessons
                 where (!l.projectID.HasValue || l.projectID == projectOne)
                       && (l.statusID == SUBMITTED || l.statusID == APPROVED)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);
        }

        public class Lesson
        {
            public int? projectID { get; set; }
            public int statusID { get; set; }
        }

        const int OTHER = 0;
        const int SUBMITTED = 1;
        const int APPROVED = 2;

        const int projectOne = 1;

        Lesson[] Lessons = new[]
                          {
                              new Lesson { projectID = null, statusID = SUBMITTED},
                              new Lesson { projectID = null, statusID = APPROVED},
                              new Lesson { projectID = null, statusID = OTHER},
                              new Lesson { projectID = 1, statusID = SUBMITTED},
                              new Lesson { projectID = 1, statusID = APPROVED},
                              new Lesson { projectID = 1, statusID = OTHER},
                              new Lesson { projectID = 2, statusID = SUBMITTED},
                              new Lesson { projectID = 2, statusID = APPROVED},
                              new Lesson { projectID = 2, statusID = OTHER},
                          };
    }
}

Here's are many of the above answers written up using xUnit.Net tests. Note that the original author's comment in the answer he selected (which is an incorrect solution) gives the results he expected. I cleaned up his solution to remove the duplicate comparisons.

Sample values are at the bottom.

    public class SelectAllInLinq2SqlTests
    {
        [Fact]
        public void Unsuccessful()
        {
            var filteredLesson =
                (from l in Lessons
                 where l.statusID == SUBMITTED ||
                       l.statusID == APPROVED &&
                       l.projectID == (l.projectID.HasValue ? projectOne : l.projectID)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);

            // this returned 5 items!
            // good - filtered the null project IDs and statusID == SUBMITTED
            // good - filtered the null project IDs and statusID == APPROVED
            // good - filtered the projectID == projectOne and statusID == SUBMITTED
            // good - filtered the projectID == projectOne and statusID == APPROVED
            // FAIL - filtered the projectID == projectTwo !!!

            // two failures first selecting all records containing a null projectID and any status (we wanted to filter on SUBMITTED or APPROVED)
            // the failure is due to operator precedence the 'l.statusID == SUBMITTED ||' is short-circuiting the rest of the comparison
        }

        [Fact]
        public void Unsuccessful2()
        {
            var filteredLesson =
                (from l in Lessons
                 where !l.projectID.HasValue ||
                       (l.statusID == SUBMITTED || l.statusID == APPROVED && l.projectID == projectOne)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);

            // this returned 6 items!
            // good - filtered the null project IDs and statusID == SUBMITTED
            // good - filtered the null project IDs and statusID == APPROVED
            // FAIL - filtered the null project IDs and statusID == OTHER!
            // good - filtered the projectID == projectOne and statusID == SUBMITTED
            // good - filtered the projectID == projectOne and statusID == APPROVED
            // FAIL - filtered the projectID == projectTwo !!!

            // Two failures:
            //   first selecting all records containing a null projectID and any status (we wanted to filter on SUBMITTED or APPROVED)
            //   second failure is due to operator precedence the 'l.statusID == SUBMITTED ||' is short-circuiting the rest of the comparison
        }

        [Fact]
        public void Unsuccessful2NotQuiteFixed()
        {
            var filteredLesson =
                (from l in Lessons
                 where !l.projectID.HasValue
                       || ((l.statusID == SUBMITTED || l.statusID == APPROVED)
                           && l.projectID == projectOne)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);
            // this returned 5 items!
            // good - filtered the null project IDs and statusID == SUBMITTED
            // good - filtered the null project IDs and statusID == APPROVED
            // FAIL - filtered the null project IDs and statusID == OTHER!
            // good - filtered the projectID == projectOne and statusID == SUBMITTED with a SUBMITTED status
            // good - filtered the projectID == projectOne and statusID == APPROVED with a APPROVED status

            // failures selecting all records containing a null projectID and any status (we wanted to filter on SUBMITTED or APPROVED)
        }

        [Fact]
        public void Sucessful()
        {
            // Questioner's provided solution...

            var filteredLesson =
                (from l in Lessons
                 where !l.projectID.HasValue && (l.statusID == SUBMITTED || l.statusID == APPROVED)
                       || l.projectID == projectOne && (l.statusID == SUBMITTED || l.statusID == APPROVED)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);
        }

        [Fact]
        public void SucessfulRefined()
        {
            // cleaned up without the duplicate status comparisons. Note the parens are necessary.

            var filteredLesson =
                (from l in Lessons
                 where (!l.projectID.HasValue || l.projectID == projectOne)
                       && (l.statusID == SUBMITTED || l.statusID == APPROVED)
                 select l).ToList();

            Assert.Equal(4, filteredLesson.Count);
        }

        public class Lesson
        {
            public int? projectID { get; set; }
            public int statusID { get; set; }
        }

        const int OTHER = 0;
        const int SUBMITTED = 1;
        const int APPROVED = 2;

        const int projectOne = 1;

        Lesson[] Lessons = new[]
                          {
                              new Lesson { projectID = null, statusID = SUBMITTED},
                              new Lesson { projectID = null, statusID = APPROVED},
                              new Lesson { projectID = null, statusID = OTHER},
                              new Lesson { projectID = 1, statusID = SUBMITTED},
                              new Lesson { projectID = 1, statusID = APPROVED},
                              new Lesson { projectID = 1, statusID = OTHER},
                              new Lesson { projectID = 2, statusID = SUBMITTED},
                              new Lesson { projectID = 2, statusID = APPROVED},
                              new Lesson { projectID = 2, statusID = OTHER},
                          };
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文