[英]subquery in LINQ query expression
Below is my sql query: 下面是我的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
I am trying to write above query into LINQ like this: 我试图像上面这样将上面的查询写到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
} ;
can anybody tell me how to achieve this 谁能告诉我如何实现这一目标
Thanks 谢谢
If Id is nullable
then you can do this: 如果Id
nullable
为nullable
则可以执行以下操作:
select new
{
Name = e.Name,
EmpId = e.Id,
ManagerId = m.Id,
DepartmentId = d.Id,
DeparmentName = d.Name,
NotExists = e.Id == null ? 1 : 0
}
You can use the let
keyword. 您可以使用
let
关键字。 It allows you to create a subqueries, assign them a name, and then use them later in your code. 它允许您创建一个子查询,为其分配一个名称,然后在以后的代码中使用它们。 Although, it's more helpful in much complex subqueries it will work here as well.
虽然,它在许多复杂的子查询中更有用,在这里也可以使用。
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
let mySub = // write your query here.
select new
{
Name = e.Name,
EmpId = e.Id,
ManagerId = m.Id,
DepartmentId = d.Id,
DeparmentName = d.Name,
NotExists = mySub
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.