我是新手加入linq到实体。我在sql server中有一个查询,并希望将其转换为LINQ to Entities。
有人可以为此提供解决方案吗?我们是否有任何在线工具将SQL查询转换为LINQ到实体?
SELECT R.ID,r.Name,u.UserId
FROM Roles R
Left JOIN UserRoles U ON r.Id = u.RoleId
AND [UserId] = '5'
where [UserId] IS NULL
答案 0 :(得分:1)
DefaultIfEmpty
会产生左外连接,因此您想要一个简单的左连接,您应该执行以下操作:
var list = (from r in context.Roles
join u in context.UsersRoles on r.Id equals u.RoleId && u.UserId='5' into x
where r.UserId == null
select new
{
r.Id,
r.Name,
u.UserId
}).ToList();
答案 1 :(得分:0)
var list = (from r in context.Roles
join ur in context.UsersRoles on r.Id equals ur.RoleId && ur.UserId='5' into x
from y in x.DefaultIfEmpty()
where r.UserId ==null
select new Object
{
Id = r.Id,
Name = r.Name,
ur.UserId
}).ToList();
注意:不了解您的第二个UserId IS NULL逻辑