使用OR语法加入使用多个列的表 [英] Join a table using on multiple columns using a OR syntax

查看:228
本文介绍了使用OR语法加入使用多个列的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要做的是: -

So what I'm trying to do is :-

SELECT * FROM TableA
JOIN TableB ON TableA.OriginPhoneNumber=TableB.Id OR TableA.DestinationPhoneNumber=TableB.Id

相当奇怪的查询我知道!但是我试图在EntityFramework / Linq中复制这个代码 - 查看所有示例,当连接使用AND(使用匿名类型)时,我可以看到一个非常简单的方法来执行此操作,但对OR连接存在相同的结果?

Rather strange query I know! But I'm trying to replicate this in EntityFramework/Linq - looking at all the samples I can see a pretty easy way to do it when the join is using an AND (using anonymous types) but does the same result exist for a OR join?

推荐答案

只要做一个交叉加入一个where子句

Just do a cross join with a where clause

var results = from a in db.TableA
              from b in db.TableB
              where a.OriginPhonenumber == b.Id 
                    || a.DestinationPhoneNumber == b.Id
              select new { A = a, B = b };

令人怀疑的是,一个或一个加入条件会比这更有效,但是很可能将导致相同的执行计划。性能除外,会得到相同的结果。

It's doubtful that an or in a join condition would be more efficient than this, but it's likely that either would result in the same execution plan. Performance aside it will give the same results.

这篇关于使用OR语法加入使用多个列的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆