LINQ to EF left join with several conditions

I am trying to replicate the following SQL using LINQ to EF but no luck.

select * from Role
left join QueueAccess on Role.RoleId = QueueAccess.RoleId and queueId = 361

Here is what I have 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 getting only the entry with the specified queueId, but none of the other entries where queueId is null.

Thank you for your help.

+3
source share
3 answers

Almost always a usage error joinin LINQ to Entities . Instead, run:

var myAccess = (((ObjectQuery)from role in entity.Role
                              where role.QueueAccess.Any(a => a.QueueId == queueId)
                              select role).Include("QueueAccess")).ToList();
+4
source

Try something like this:

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
source

- , 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 )
0

Source: https://habr.com/ru/post/1730484/


All Articles