I\'m trying to use entity to do this:
SELECT *
FROM aspnet_Users u LEFT JOIN (SELECT u.UserId, p.Permission, p.MediaId, p.Valid
Had a similar problem, I managed to solve it by moving the null check outside of the sql. In your case you can do it by calling ToList()
before checking for Null
:
var intermediateUl = from us in en.aspnet_Users
join pm in up
on us.UserId equals pm.UserId into pmOuter
from pm2 in pmOuter.DefaultIfEmpty()
select new { us.UserName, us.UserId, pm2};
var ul = intermediateUl
.ToList()
.Select(o => new PermissionInfo {
Permission = (o.pm2 == null ? -1 : pm2.Permission1),
PermissionId = (o.pm2 == null ? -1 : pm2.PermissionId)
UserName = o.UserName,
UserId = o.UserId,
});
The problem surprised me as well, because couple lines above I had a similar query with outer join which passed OK. It seems that checking for null with linq outer construct is only possible with entities known by Entity Framework. Any other class, anonymous or not, confuses EF/linq.
I know this question is old, but maybe it will help someone :)