我有PM收件箱和2个表:用户和管理员。
我有这个问题:
return (from pm in dc.PrivateMessages
join user in dc.Users
on pm.Sender equals user.UserID
select new PMInbox
{
SenderUsername = user.Username
}).ToList();
但我只能拥有来自用户发件人的所有PM。
我必须在这里添加哪种联接才能拥有pm.Sender == user.UserID或pm.Sender == admin.AdminID?
答案 0 :(得分:4)
从逻辑上讲,这就是:
var messageUsers = dc.PrivateMessages.Join(dc.Users,
pm => pm.Sender,
user => user.UserId,
(pm, user) => user);
var messageAdmins = dc.PrivateMessages.Join(dc.Users,
pm => pm.Sender,
admin => admin.AdminID,
(pm, admin) => admin);
return messageUsers.Union(messageAdmins)
.Select(user => new PMInbox {
SenderUsername = user.Username
})
.ToList();
或者你可以使其成为与where子句的交叉连接:
return (from pm in dc.PrivateMessages
from user in dc.Users
where pm.Sender == user.UserID || pm.Sender == user.AdminID
select new PMInbox
{
SenderUsername = user.Username
}).ToList();