Entity Framework: The argument to DbIsNullExpression must refer to a primitive or reference type

后端 未结 2 1292
慢半拍i
慢半拍i 2021-01-16 20:07

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
                              


        
2条回答
  •  甜味超标
    2021-01-16 21:01

    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 :)

提交回复
热议问题