我正在尝试使用实体来执行此操作:
SELECT *
FROM aspnet_Users u LEFT JOIN (SELECT u.UserId, p.Permission, p.MediaId, p.Valid
FROM aspnet_Users u LEFT JOIN Permission p ON u.UserId = p.UserId
WHERE p.MediaId = 57 AND p.Valid = 1
) B
ON u.UserId = B.UserId
这是 C# 代码:
var up = from u in en.aspnet_Users
join p in en.Permissions
on u.UserId equals p.UserId into pu
from p2 in pu.DefaultIfEmpty()
where p2.MediaId == this.MediaId && p2.Valid == true
select new
{
u.UserId,
p2.PermissionId,
p2.Permission1,
p2.MediaId,
p2.Valid
};
var ul = from us in en.aspnet_Users
join pm in up
on us.UserId equals pm.UserId into pm1
from pm2 in pm1.DefaultIfEmpty()
orderby us.LoweredUserName
select new PermissionInfo {
Permission = (pm2 == null ? -1 : pm2.Permission1),
UserName = us.UserName,
UserId = us.UserId,
PermissionId = (pm2 == null ? -1 : pm2.PermissionId) };
ret = ul.ToList();
但是,在最后一行得到了他的错误
DbIsNullExpression 的参数必须引用原始类型或引用类型。
任何人都知道如何解决这个问题?谢谢。
编辑以回答 1 个建议:
var up = from u in en.aspnet_Users
join p in en.Permissions
on u.UserId equals p.UserId into pu
from p2 in pu.DefaultIfEmpty()
where p2.MediaId == this.MediaId && p2.Valid == true
select new {u, p2};
var ul = from us in en.aspnet_Users
join pm in up
on us.UserId equals pm.u.UserId into pm1
from pm2 in pm1.DefaultIfEmpty()
orderby us.LoweredUserName
select new PermissionInfo
{
Permission = (pm2 == null ? -1 : pm2.p2.Permission1),
UserName = us.UserName,
UserId = us.UserId,
PermissionId = (pm2 == null ? -1 : pm2.p2.PermissionId)
};
ret = ul.ToList();