开发者

Linq2Sql, Joining on possible null fields

开发者 https://www.devze.com 2023-01-25 00:04 出处:网络
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

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()
jo开发者_StackOverflow社区in 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消