0

我正在尝试使用实体来执行此操作:

    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();
4

2 回答 2

2

有一个类似的问题,我设法通过将空检查移到sql之外来解决它。在您的情况下,您可以ToList()在检查之前致电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, 
     });

这个问题也让我感到惊讶,因为上面的几行我有一个类似的外连接查询,它通过了。似乎只有实体框架已知的实体才能使用 linq 外部构造检查 null。任何其他类,无论是否匿名,都会混淆 EF/linq。

我知道这个问题很老,但也许它会帮助某人:)

于 2014-01-17T14:52:31.573 回答
0

我意识到,由于在第二个查询中,它仍然有与 aspnet_Users 表的左连接,所以在第一个查询中不需要 u,所以我将其更改为下面,它现在可以工作了。但是,很高兴知道为什么原始代码和建议的代码会失败。

    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 p2;

            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();
于 2012-10-08T15:03:37.937 回答