我正在尝试将SQL查询转换为linq,但不像在本示例SQL查询中那样,不知道如何在join语句内使用AND子句。
我在下面突出了我无法弄清的部分。
DECLARE @GroupId INT = 10
SELECT
U.UserName,
U.UserID,
@GroupId AS "GroupID",
CASE WHEN GU.GROUPID IS NULL THEN 0
ELSE 1 END
AS InGroup
FROM Users U
LEFT JOIN GroupUsers GU on GU.UserID = U.UserID
AND GU.GROUPID = @GroupId -- HERE!!
这跟我已经接近
from user in Users
join gu in GroupUsers on user.UserID equals gu.UserID into subq
from sub in subq.DefaultIfEmpty()
where sub.GroupID == 10 || sub == null
select new
{
Username = user.UserName,
UserId = user.UserID,
GroupId = 10,
InGroup = sub != null
}
但它使用
WHERE
删除数据。 最佳答案
您可以将GroupId
条件嵌入查询中:
from user in Users
join gu in GroupUsers.Where(gu => gu.GroupID == 10)
on user.UserID equals gu.UserID into subq
from sub in subq.DefaultIfEmpty()
select new
{
Username = user.UserName,
UserId = user.UserID,
GroupId = 10,
InGroup = sub != null
}
关于c# - 如何在LINQ Join中使用AND,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26060228/