1

我正在尝试转换此查询(已经工作)

select [User].ID AS ID_USER
from [User]
right join Building on Building.ID = 4 AND
                       Building.ID_USER_RESPONSIBLE <> [User].ID
where [User].ID_MANAGER = 1

到Linq to SQL,但我不知道我做错了什么。看看我的努力

IList<User> lstUser = (from building in db.GetTable<Building>()
                       join user in db.GetTable<User>()
                       on new
                       {
                           ID_BUILDING = building.ID,
                           ID_USER = building.ID_USER_RESPONSIBLE
                       }
                       equals new
                       {
                           ID_BUILDING = 4,
                           ID_USER != user.ID
                       } into grpUser
                       from grp in grpUser.DefaultIfEmpty()
                       where building.ID_MANAGER = 1
                       select new 
                       {
                           ID_USER = 
                        });

我需要退回所有用户以及不对任何建筑物负责的用户;

编辑

此处发布的任何解决方案都有效。我已经解决了这样做:

 var lstUser = (from building in db.GetTable<Building>()
                join user in db.GetTable<User>()
                on building.ID equals 4
                where user.ID_MANAGER == 1 &&
                      building.ID_USER_RESPONSIBLE != user.ID
                select new 
                {
                       ID_USER = user.ID,
                       NAME = user.NAME
                }).ToList();
4

1 回答 1

0

看看以下是否有效:

IList<User> lstUser = (from building in db.GetTable<Building>()
                       join user in db.GetTable<User>()
                       on new
                       {
                           ID_BUILDING = building.ID,
                           ID_USER = building.ID_USER_RESPONSIBLE,
                           building.ID_MANAGER
                       }
                       equals new
                       {
                           ID_BUILDING = 4,
                           ID_USER = user.ID,
                           1
                       } into grpUser
                       from grp in grpUser.DefaultIfEmpty()
                       select new 
                       {
                           ID_USER = grp.ID
                        });

话虽如此,您的要求似乎存在差距,“我需要退回所有用户以及不对任何建筑物负责的用户;” 我可能误读了您的声明,但不是不负责构建较大“所有用户”集的子集的用户列表。因此,无论他们的建筑物成员身份如何,您总是只会获得所有用户。

于 2013-07-22T19:18:46.543 回答