没有外键的实体框架连接
我有一个消息表,其中每行都有发送该消息的用户的 ID。 但我无法编辑数据库,并且没有外键。 没有任何关系可以加入吗?
var msgs = (from m in dbContext.messages
join a in dbContext.users on m.userid equals a.id into sender
where (m.date > LastReceivedDate)
orderby m.date
select new
{
Sender = sender.FirstOrDefault(),
Message = m
})
这就是我的代码,它可以运行,但不会返回任何内容。当我取消连接时,我得到了结果。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了我对 Craig Stuntz 的 答案,如果您放弃
,您可以消除第二个查询,并在单个查询中将其全部返回。 FirstOrDefault()
并且可以在连接没有出现用户时处理 Sender 可能为null
的情况。In addition to my comment on Craig Stuntz's answer, you can get the 2nd query eliminated and have it all returned in a single query if you forgo the
.FirstOrDefault()
and can deal with Sender possibly beingnull
if the join turns up no users.