これを行うためにエンティティを使用しようとしています:
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();