SQL到LiNQ,左联接,联接条件

时间:2019-02-08 14:37:10

标签: sql linq

我无法将SQL转换为LINQ。问题是左联接的条件。我不适合LINQ。

我已经尝试过了。但这是不正确的。我需要在左连接处!u.Inactive

from p in db.Persons
join u in db.Users on p.Id equals u.Pid into x
from u in x.DefaultIfEmpty()
where p.Cid == cid && !p.Deleted && !u.Inactive
orderby p.Name ascending

SQL:

SELECT P.ID, P.Name P.EmpNo, U.ID, U.Class, P.Phone, P.Email 
FROM Persons AS P 
LEFT JOIN Users AS U ON P.ID = U.PID AND U.Inactive <> 1
WHERE P.CID = 1234 AND P.Deleted <> 1
ORDER BY P.Name 

1 个答案:

答案 0 :(得分:5)

尝试:

from p in db.Persons
join u in db.Users.Where(user => !user.Inactive) on p.Id equals u.Pid into x
from u in x.DefaultIfEmpty()
where p.Cid == cid && !p.Deleted 
orderby p.Name ascending