具有多个条件的 LINQ to EF 左连接
我尝试使用 LINQ to EF 复制以下 SQL,但没有成功。
select * from Role
left join QueueAccess on Role.RoleId = QueueAccess.RoleId and queueId = 361
这是我尝试过的。
var myAccess = (from role in entity.Role.Include(p => p.QueueAccess)
join qa in entity.QueueAccess
on new { rID = role.RoleId, qID = queueId } equals new { rID = qa.RoleId, qID = qa.QueueId }
select role).ToList();
也尝试过这个。
var myAccess = entity.Role.Include(p => p.QueueAccess)
.Where(x => x.QueueAccess.Any(a => a.QueueId == queueId)).ToList();
我继续只获取具有指定queueId的记录,但没有获取queueId为空的其他记录。
感谢您的帮助。
I am trying to replicate the following SQL using LINQ to EF but with no luck.
select * from Role
left join QueueAccess on Role.RoleId = QueueAccess.RoleId and queueId = 361
Here's what I've tried.
var myAccess = (from role in entity.Role.Include(p => p.QueueAccess)
join qa in entity.QueueAccess
on new { rID = role.RoleId, qID = queueId } equals new { rID = qa.RoleId, qID = qa.QueueId }
select role).ToList();
Also tried this.
var myAccess = entity.Role.Include(p => p.QueueAccess)
.Where(x => x.QueueAccess.Any(a => a.QueueId == queueId)).ToList();
I keep on getting only the record with the specified queueId but none of the other records where the queueId is null.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 LINQ to Entities 中使用
join
几乎总是错误的。相反,请执行以下操作:It's nearly always a mistake to use
join
in LINQ to Entities. Instead, do:尝试这样的事情:
Try something like this:
类似的方法也有效,将条件放在 ON 中,而不是放在 WHERE 子句中。
Something like this works too, puts the condition in the ON as oppose to the WHERE clause.