在职的:
我正在尝试在我的Contacts
表和我的Permissions
表之间执行 LEFT OUTER JOIN。我有这个正常工作的基础,无论他们是否有相应的权限,都可以取回联系人列表。
// Query for Contacts
from contact in Contacts
join permission in Permissions on contact.Id equals permission.ObjectId into permissionGrp
from p in permissionGrp.DefaultIfEmpty()
where p==null || (p!=null && /* ... condition based on the existence of a permission */)
select new { contact, permission = p };
生成 WHERE SQL:
WHERE
(t1.PermissionId IS NULL OR
((t1.PermissionId IS NOT NULL AND ... other conditions ... )))
问题:
我想修改上述内容以引入“后备”检查;没有按预期工作。
要求:
- 当没有
Permission
对应的Contact
(iep==null
) 时,则只包含基于预定bool
值的行allowed
。
试图:
我以为我可以这样做where (p==null && allowed) || ...
:
// Fallback permission
bool allowed = false;
// Query for Contacts
from contact in Contacts
join permission in Permissions on contact.Id equals permission.ObjectId into permissionGrp
from p in permissionGrp.DefaultIfEmpty()
/* Added bool condition 'allowed' to control inclusion when 'p' is null */
where (p==null && allowed) || (p!=null && /* ... condition based on the existence of a permission */)
select new { contact, permission = p };
预期的:
何时allowed = false
(不接受null
许可)
WHERE
((t1.PermissionId IS NOT NULL AND ... other conditions ... ))
何时allowed = true
(接受null
许可)
WHERE
(t1.PermissionId IS NULL OR
((t1.PermissionId IS NOT NULL AND ... other conditions ... )))
实际结果:
总是输出好像allowed=false
什么时候true
?
WHERE
((t1.PermissionId IS NOT NULL AND ... other conditions ... ))
概括:
我希望我只是在做一些很容易解决的愚蠢的事情。
如何null
根据给定bool
值过滤我的值记录?