可以为 null 的 Linq 表达式

发布于 2025-01-04 23:11:11 字数 431 浏览 4 评论 0原文

看起来是个愚蠢的问题,但我就是不明白。 我的实体:

public class Page
{
    public int Id { get; set; }
    //...
    public int? ParentId { get; set; }
}

在控制器中:

db.Pages.First(x => x.ParentId == null);

按预期工作(返回一些元素)。 但是:

int? test = null;
db.Pages.First(x => x.ParentId == test);

抛出 Sequence contains no elements

我错过了什么?

Looks like stupid question, but I just dont get it.
My entity:

public class Page
{
    public int Id { get; set; }
    //...
    public int? ParentId { get; set; }
}

In controller:

db.Pages.First(x => x.ParentId == null);

Works as expected (returns some element).
But:

int? test = null;
db.Pages.First(x => x.ParentId == test);

Throws Sequence contains no elements

What do I miss?

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

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

发布评论

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

评论(3

小傻瓜 2025-01-11 23:11:11

我相信某些 LINQ 提供程序的空值存在奇怪之处。尝试:

var query = db.Pages.First(x => (test != null && x.ParentId == test) ||
                                (test == null && x.ParentId == null));

或者,针对不同的情况使用不同的查询:

var query = test == null ? db.Pages.First(x => x.ParentId == null)
                         : db.Pages.First(x => x.ParentId == test);

基本上这是因为 SQL 将 NULL 视为不等于其自身,因此:

WHERE X = Y

如果 X 和 Y 均为空值,仍然会失败。使用 == null 部分(带有文字 null)强制转换为 ISNULL 或任何 SQL 等效项。

我同意这很痛苦,其他人可能有更好的解决方法,但这可能会帮助您继续前进。

I believe there's an oddity around nulls with some LINQ providers. Try:

var query = db.Pages.First(x => (test != null && x.ParentId == test) ||
                                (test == null && x.ParentId == null));

Alternatively, use different queries for the different situations:

var query = test == null ? db.Pages.First(x => x.ParentId == null)
                         : db.Pages.First(x => x.ParentId == test);

Basically this is because SQL treats NULL as unequal to itself, so:

WHERE X = Y

will still fail if both X and Y are null values. Using the == null part (with a literal null) forces a conversion to ISNULL or whatever the SQL equivalent is.

I agree it's a pain, and someone else may have a better workaround, but this may help you get going.

紫竹語嫣☆ 2025-01-11 23:11:11

您可以执行类似的操作作为解决方法:

int? test = null;
if(test.HasValue) {
 db.Pages.First(x => x.ParentId == test.Value);
} else {
 db.Pages.First(x => x.ParentId == null);
}

我假设由于 int? 实际上是一个 Nullable 我们的 linq-to-entities 提供程序不会比较事物正确的。

You could do something like this as a workaround:

int? test = null;
if(test.HasValue) {
 db.Pages.First(x => x.ParentId == test.Value);
} else {
 db.Pages.First(x => x.ParentId == null);
}

I'm assuming since int? is actually a Nullable<int> our linq-to-entities provider isn't comparing things right.

蘸点软妹酱 2025-01-11 23:11:11

试试这个(根据 gdoron 的评论进行修改。现在正是 gideon 发布的内容,所以请接受他的而不是我的):

int? test = null;
if(test.HasValue) {
    db.Pages.First(x => x.ParentId == test.Value);
} else {
    db.Pages.First(x => x.ParentId == null);
}

Try this (modified according to gdoron's comment. It now is exactly what gideon posted, so please accept his instead of mine):

int? test = null;
if(test.HasValue) {
    db.Pages.First(x => x.ParentId == test.Value);
} else {
    db.Pages.First(x => x.ParentId == null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文