具有多个条件的 LINQ to EF 左连接

发布于 2024-08-20 01:09:47 字数 670 浏览 9 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云巢 2024-08-27 01:09:47

在 LINQ to Entities 中使用 join 几乎总是错误的。相反,请执行以下操作:

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

It's nearly always a mistake to use join in LINQ to Entities. Instead, do:

var myAccess = (((ObjectQuery)from role in entity.Role
                              where role.QueueAccess.Any(a => a.QueueId == queueId)
                              select role).Include("QueueAccess")).ToList();
℉絮湮 2024-08-27 01:09:47

尝试这样的事情:

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 };

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 };
空宴 2024-08-27 01:09:47

类似的方法也有效,将条件放在 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 )

Something like this works too, puts the condition in the ON as oppose to the WHERE clause.

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文