Linq2Sql,加入可能的空字段

发布于 2024-10-02 07:02:22 字数 632 浏览 3 评论 0 原文

我正在尝试执行一个可以连接可能为空值的连接。

from data in Data
join d1 in Data on new { a = data.pId, b = language } equals new { a = d1.pId, b = d1.language } into j1
from dta1 in j1.DefaultIfEmpty()
join d2 in Data on new { a = data.pId, b = fallback} equals new { a = d2.pId, b = d2.language } into j2
from dta2 in j1.DefaultIfEmpty()
where ((int?)dta1.id ?? dta2.id) == data.Id
select data;

这里的问题是 pId 可以为 null,如果是,则连接在比较时不会匹配,

data.pId = null

而不是

data.pId is null

在正常的 where 子句中,我可以执行 object.Equals(data.pId, d1.pId) ,它会做空检查,但是我如何在连接中执行此操作?

I am trying to do a join that could join on a possible null value.

from data in Data
join d1 in Data on new { a = data.pId, b = language } equals new { a = d1.pId, b = d1.language } into j1
from dta1 in j1.DefaultIfEmpty()
join d2 in Data on new { a = data.pId, b = fallback} equals new { a = d2.pId, b = d2.language } into j2
from dta2 in j1.DefaultIfEmpty()
where ((int?)dta1.id ?? dta2.id) == data.Id
select data;

The problem here is that pId can be null, and if it is, the join won't match as it compares

data.pId = null

instead of

data.pId is null

In a normal where clause I could do object.Equals(data.pId, d1.pId) and it would do a null check, but how can I do this in a join?

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

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

发布评论

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

评论(1

兮颜 2024-10-09 07:02:22

一种方法是将空值转换为其他一些未使用但可等同的值。

join d1 in Data
  on new { a = data.pId ?? -33, b = language }
  equals new { a = d1.pId ?? -33, b = d1.language }
  into j1

One approach is to convert the null value into some other unused, yet equatable value.

join d1 in Data
  on new { a = data.pId ?? -33, b = language }
  equals new { a = d1.pId ?? -33, b = d1.language }
  into j1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文