要指定我想要的内容,这是应该完成工作的SQL查询:
SELECT user
FROM User AS user
JOIN UserAssignment AS userAssignment ON userAssignment.UserID = user.ID
JOIN GroupAssignment AS groupAssignment ON user.ASSIGNMENT_ID = groupAssignment.Id ---> this is what I can't do in LINQ, because the database columns don't align with the POCO properties (db: user has the association, POCO: groupAssignment has the association)
WHERE groupAssignment.ClientSIte == \*theGivenClientsite*\ ||
WHERE userAssignment.ClientSIte == \*theGivenClientsite*\
现在我想要的不是SQL字符串,而是类型安全。在大多数情况下,我一直在使用LINQ to NHibernate,因为它非常相似。
问题在于第4行:加入GroupAssignment ....
这意味着,如果我使用LINQ to NHibernate,我必须在包含user的groupAssignment.Users上加入用户和组分配。
这就是我被困的地方。这就是我尝试的内容(注释掉的位是我单独尝试的东西,两者都没有用):
List <User> users = (List<User>)
(from user in session.Query<User>()
join userAssignment in session.Query<UserAssignment>() on user equals userAssignment.User
join groupAssignment in session.Query<GroupAssignment>() on //user equals groupAssignment.Users.First(u => u.Equals(user))
//where groupAssignment.Users.Any(u => u.Equals(user)) &&
groupAssignment.ClientSite == clientSite ||
userAssignment.ClientSite == clientSite
select user
).ToList();
有人可以帮忙吗?使用QueryOver()或其他任何不仅仅是SQL字符串的解决方案将不胜感激!
编辑:这是我顽固的解决方案:
IList<UserInfo> usersViaGroupAssignment = (IList<UserInfo>)
session.Query<GroupAssignment>()
.Where(ga => ga.ClientSite.Equals(clientSite))
.Select(ga => ga.Users)
.SelectMany(x => x).ToList();
IList<UserInfo> usersViaUserAssignment = (IList<UserInfo>)
session.Query<UserAssignment>()
.Where(ua => ua.ClientSite.Equals(clientSite))
.Select(ua => ua.User)
.ToList();
// This removing of duplicates might be inefficient if there is a large amount of assignments concerning the same users.
List<UserInfo> allUsers = usersViaGroupAssignment.Concat(usersViaUserAssignment).Distinct().ToList();