EF Linq无法将sql查询转换为正确的linq

时间:2013-11-01 18:11:14

标签: c# linq entity-framework

我有以下sql查询,我无法将其翻译为正确的linq语句。

SELECT 
    r.RoleName,
    uir.UserId
FROM 
    webpages_Roles r
         LEFT JOIN webpages_UsersInRoles uir ON uir.RoleId = r.RoleId 
                                                  AND r.AppId = 1
WHERE
     uir.UserId IS NULL

在我的c#中,我有类似的东西:

IEnumerable<Role> roles = uow.RolesRepository.Get(r => r.AppId == 1 && r.UsersInRoles.Any(uir => uir.UserId == null), includeProperties: "UsersInRoles");

我正在使用存储库,但也可能是:

IEnumerable<Role> roles = context.Roles.Where(r => r.AppId == 1 && r.UsersInRoles.Any(uir => uir.UserId == null));

1 个答案:

答案 0 :(得分:1)

尝试:

Enumerable roles = context.Roles
    .Where(r => r.AppId == 1 && !r.UsersInRoles.Any(uir => uir.UserId != null));