[c#]LINQ 查询表达式中的子查询

标签: Linq C#
发布时间: 2017/2/28 21:17:22
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

下面是我的 sql 查询︰

select e.id, m.id, e.Name, (select 1 where e.Id Is Null) as NotExists
    from    Employee e 
            INNER JOIN Department d
            ON e.Id = d.Id  
            LEFT JOIN Manager m
            ON e.Id = m.Id
            order by e.Name

我试图写入上面查询 LINQ 像这样︰

var result = from e in employeeRep
             join d in departmentRep
             on e.Id equals d.Id
             join m in manager
             on e.Id = m.Id
             order by e.Name
             select new
             {
               Name = e.Name,
               EmpId = e.Id,
               ManagerId = m.Id,
               DepartmentId = d.Id,
               DeparmentName = d.Name,
               NotExists = // here I want to write this query (select 1 where e.Id Is Null) as NotExists
             } ;

谁能告诉我如何实现这一目标呢

谢谢你

解决方法 1:

如果Idnullable 然后你可以这样做︰

select new
             {
               Name = e.Name,
               EmpId = e.Id,
               ManagerId = m.Id,
               DepartmentId = d.Id,
               DeparmentName = d.Name,
               NotExists = e.Id == null ? 1 : 0 

            }
官方微信
官方QQ群
31647020