我正在尝试使用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为null的其他记录。
感谢您的帮助。
答案 0 :(得分:4)
It's nearly always a mistake to use join
in LINQ to Entities。相反,做:
var myAccess = (((ObjectQuery)from role in entity.Role
where role.QueueAccess.Any(a => a.QueueId == queueId)
select role).Include("QueueAccess")).ToList();
答案 1 :(得分:2)
尝试这样的事情:
var access = from role in Role
join oq in (from q in QueueAccess
where q.queueId = 361
select q) on role.RoleId equals queue.RoleId into oqs
from queue in oqs.DefaultIfEmpty()
select new { role.RoleId, queue.Property };
答案 2 :(得分:0)
这样的东西也有效,将条件置于ON中,与WHERE子句相反。
join tbl3 in model.phone.Where(p => p.queue == 0 && p.phnkey == key) on x.key equals tbl3.y into a3
from phn in a3.DefaultIfEmpty()
where (phn.abc == 0 )