【问题标题】:Check for null condition for left side table in left join in Linq检查Linq左连接中左侧表的空条件
【发布时间】:2019-04-10 13:08:58
【问题描述】:

我在 linq 查询中使用左连接。我需要检查 ON 子句中的左表值是否为空。

   join x in employeee on u.id equals x.userId into ux 
   from ujoinx in ux.DefaultIfEmpty()
   join y in department on ujoinx.id equals y.employeeId into xy 
   from xjoiny in xy.DefaultIfEmpty()
   select new {
      EmployeeSal = ujoinx!=null?ujoinx.employeeSal:0, 
      EmployeeTax = ujoinx!=null?ujoinx.employeeTax:0, 
      UserName = u.username,
      DeptName = xjoiny!=null?xjoiny.name:""         
   }

这里我要检查 ujoinx 在这个连接条件下是否为空。 join y in department on ujoinx.id equals y.employeeId into xy

有没有可能用ujoinx.id equals y.employeeId检查空条件?

【问题讨论】:

  • 为什么不在连接前添加 where 子句?
  • 我试过 join y in department where ujoinx != null on ujoinx.id equals y.employeeId into xy 。它不工作
  • 有什么解决办法吗?

标签: c# linq left-join


【解决方案1】:

在您的评论中,您将where 子句放在了错误的位置。您应该在 join:

之前过滤
from ujoinx in ux.DefaultIfEmpty()
    where ujoinx != null
    join y in department on ujoinx.id equals y.employeeId
    into xy

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多