我有以下查询:
var poll = entities.Polls.Join(entities.Descriptors, p => p.DescriptorID, q => q.ID, (p, q) => new
{
Poll = p,
Descriptor = q
}).Join(entities.Media1, p => p.Descriptor.MediaID, q => q.ID, (p, q) => new
{
Poll = p.Poll,
Descriptor = p.Descriptor,
Media = q
}).Join(entities.PollCommentSettings,p => p.Poll.ID,q => q.PollID,(p,q) => new
{
Poll = p.Poll,
Descriptor = p.Descriptor,
Media = p.Media,
CommentSettings = q
}).FirstOrDefault(p => p.Poll.ID == request.PollID);
我遇到的问题是两个正在连接的表可能没有对象,但我不在乎,并且仍想创建我的顶级对象。
如果没有匹配项,两个PollCommentSettings和Media1表会导致返回任何内容。我需要的是最终对象,如果找不到它们,只需将它们设置为null。我相信这是对这两个表格的正确外部联接。
答案 0 :(得分:1)
我想通过在LINQpad中尝试一些东西,我想出了如何完成我需要的东西。我得到了以下查询,我相信它能完成我想要的任务。
from poll in Polls join descriptor in Descriptors on poll.DescriptorID equals descriptor.ID into pdj
from descriptor in pdj.DefaultIfEmpty()
join media in Media1 on descriptor.MediaID equals media.ID into dmj
from media in dmj.DefaultIfEmpty()
join commentSetting in PollCommentSettings on poll.ID equals commentSetting.PollID into pcj
from commentSetting in pcj.DefaultIfEmpty()
where poll.ID == 28
select new
{
poll,
descriptor,
media,
commentSetting
}