c# Linq 如何使用 if 条件编写 Inner Join
c# Linq How to write Innter Join using if condition
这里我有两个 Tables Like
员工
Id IsAccepected(bool) AccepectedBy EmpId
1 0 0 E1-1
2 1 2 E1-2
3 1 1 C1-1
登录
Id name
1 John
2 Mick
3 smith
这是我的问题,如果 IsAccepected==True 那么我需要从登录中获取已接受的记录 Table
var x=(from n i in _db.EMployee
WHERE n.Empid='E1-1'
select n).Tolist();
foreach(var item in x){
if(item.IsAccepected==True){
.......
}
但我不需要所有这些是否可以在 Linq 中编写此条件
像下面这样的东西应该可以工作:
var x = from e in _db.Employee join l in _db.Login on e.AcceptedBy equals l.ID where e.Accepted == true select l;
我猜你需要这个 sql 查询需要在 linq 中对吗?
select b1.*,isnull((select 1 from A as a1 where a1.AccepectedBy=b1.Id and a1.EmpId='E1-2'),0) as isaccepted from B as b1
通过左连接方法使用此代码-
select Login.name,Employee.EmpId from Employee left join Login on Employee.AccepectedBy=Login.Id where Employee.[IsAccepected(bool)]=1
这里我有两个 Tables Like 员工
Id IsAccepected(bool) AccepectedBy EmpId
1 0 0 E1-1
2 1 2 E1-2
3 1 1 C1-1
登录
Id name
1 John
2 Mick
3 smith
这是我的问题,如果 IsAccepected==True 那么我需要从登录中获取已接受的记录 Table
var x=(from n i in _db.EMployee
WHERE n.Empid='E1-1'
select n).Tolist();
foreach(var item in x){
if(item.IsAccepected==True){
.......
}
但我不需要所有这些是否可以在 Linq 中编写此条件
像下面这样的东西应该可以工作:
var x = from e in _db.Employee join l in _db.Login on e.AcceptedBy equals l.ID where e.Accepted == true select l;
我猜你需要这个 sql 查询需要在 linq 中对吗?
select b1.*,isnull((select 1 from A as a1 where a1.AccepectedBy=b1.Id and a1.EmpId='E1-2'),0) as isaccepted from B as b1
通过左连接方法使用此代码-
select Login.name,Employee.EmpId from Employee left join Login on Employee.AccepectedBy=Login.Id where Employee.[IsAccepected(bool)]=1