Linq-Sql IQueryable和链接 OR 运算

发布于 2024-08-13 17:41:29 字数 1277 浏览 10 评论 0原文

我正在尝试模拟:

WHERE x.IsActive = true OR x.Id = 5

以下导致使用“AND”...如何使用 IQueryable (qry) 和我的可空 int 模拟“OR”条件,考虑到可能涉及其他过滤(如此处的 IsActive 过滤器)?

            if (onlyActiveItems) //bool
            {
                qry = qry.Where(x => x.IsActive == true);
            }

            if (whenSpecifiedMustIncludeRecordWithThisId.HasValue) //int?
            {
                qry = qry.Where(x => x.Id == whenSpecifiedMustIncludeRecordWithThisId.Value);
            }

我考虑过联合,但似乎这个问题的答案应该简单得多。


这是一种解决方案,可以解决我在尝试组合所有答案时遇到的“可为空对象必须有一个值”的问题。否则,是什么导致可为 null 的值在为 null 时被求值?

            if (whenSpecifiedMustIncludeRecordWithThisId.HasValue)
            {
                qry = qry.Where(x => (!onlyActiveItems || (onlyActiveItems && x.IsActive)) || x.Id == whenSpecifiedMustIncludeRecordWithThisId.Value);
            }
            else
            {
                qry = qry.Where(x => (!onlyActiveItems || (onlyActiveItems && x.IsActive)));
            }

在某些情况下,使用 nullable 的 .Value 属性似乎也会产生影响,正如我在此处的另一个问题Linq to SQL Int16 在 SQL 命令中转换为 Int32

I'm trying to simulate:

WHERE x.IsActive = true OR x.Id = 5

The following causes 'AND' to be used... how do I simulate an 'OR' condition with IQueryable (qry) and my nullable int, given that other filtering might be involved as with the IsActive filter here?

            if (onlyActiveItems) //bool
            {
                qry = qry.Where(x => x.IsActive == true);
            }

            if (whenSpecifiedMustIncludeRecordWithThisId.HasValue) //int?
            {
                qry = qry.Where(x => x.Id == whenSpecifiedMustIncludeRecordWithThisId.Value);
            }

I have considered union but its seems the answer to this should be much simpler.


This is one solution which gets around the problem I get with "Nullable object must have a value" when trying the combined all in one answer. What causes the nullable to be evaluated when it is null otherwise?

            if (whenSpecifiedMustIncludeRecordWithThisId.HasValue)
            {
                qry = qry.Where(x => (!onlyActiveItems || (onlyActiveItems && x.IsActive)) || x.Id == whenSpecifiedMustIncludeRecordWithThisId.Value);
            }
            else
            {
                qry = qry.Where(x => (!onlyActiveItems || (onlyActiveItems && x.IsActive)));
            }

It seems also in some cases the use of the nullable's .Value property makes a difference as seen in another question of mine here Linq to SQL Int16 Gets Converted as Int32 In SQL Command

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

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

发布评论

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

评论(2

jJeQQOZ5 2024-08-20 17:41:29

试试这个:

qry = qry.Where(x => (onlyActiveItems
                      ? x.IsActive
                      : false) ||
                     (whenSpecifiedMustIncludeRecordWithThisId.HasValue
                      ? x.Id == whenSpecifiedMustIncludeRecordWithThisId
                      : false) ||
                     (!onlyActiveItems && !whenSpecifiedMustIncludeRecordWithThisId.HasValue));

请注意,我们正在比较一个 int? 和一个 int,而不是两个 int

我在这里假设查询的目的是过滤掉是否满足某些条件。

  • 如果 onlyActiveItems 为 true,则验证 IsActive 字段是否为 true
  • 如果 whenSpecifiedMustIncludeRecordWithThisId.HasValue 为 true,则验证该值是否与 Id 字段匹配
  • 如果两者都为 true,则逻辑上为 OR 如果两个条件
  • 都为 false,则显示所有记录(如果这不是本意,您可以删除最后一个条件)

Try this:

qry = qry.Where(x => (onlyActiveItems
                      ? x.IsActive
                      : false) ||
                     (whenSpecifiedMustIncludeRecordWithThisId.HasValue
                      ? x.Id == whenSpecifiedMustIncludeRecordWithThisId
                      : false) ||
                     (!onlyActiveItems && !whenSpecifiedMustIncludeRecordWithThisId.HasValue));

Note that we're comparing an int? to an int, not two ints.

I am assuming here that the point of the query is to filter out if certain conditions are met.

  • If onlyActiveItems is true, it verifies whether the IsActive field is true
  • If whenSpecifiedMustIncludeRecordWithThisId.HasValue is true it verifies whether the value matches the Id field
  • If both are true it will logically OR the conditions
  • If both are false all records are displayed (if this is not the intent, you can remove the last condition)
感受沵的脚步 2024-08-20 17:41:29

当使用“int?”时我通常使用 object.Equals(i1, i2) 来比较它们,例如

from r in cxt.table
where object.Equals(r.column, nullableInt)
select r

这可以避免所有可为空的问题。

When working with "int?" I usually compare them using object.Equals(i1, i2), for example

from r in cxt.table
where object.Equals(r.column, nullableInt)
select r

This avoids all nullable issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文