linq连接3个表或条件

我需要在LINQ中创建一个带有3个表和OR条件的语句。

我的函数接收一个整数,我们称之为intZ 。 我有3个表: tableAtableBtableC

tableA具有列int1int2intBintBtableB相关。

问题: tableA int1int2可以是intZ ,它必须与一个tableC记录匹配。

我需要OR条件,但我不知道在哪里放置它。 它是否在where子句中? 还是在equals子句中?

目前,我知道如何加入3张桌子,但条件是杀了我。

在linq中创建语句的两种方法有什么区别? 是否会对性能产生影响?

编辑:好的,现在我觉得它更清楚了。 intZ必须与intCtableC相关,并且此数字可以是tableA int1int2

在此处输入图像描述

只需将其添加到Where 。 在Linq2Sql中,这将被转换为tableB上的内连接(带或)

 from a in tableA from b in tableB.Where(x => xA == aA || xB == aB) select new { a, b }; 

你不能在LINQ的连接中使用“或”条件,因为它只支持equijoins。 但是你应该可以在where子句中完成它而没有任何问题。 例如:

 var query = from rowC in tableC where rowC.intC == intZ from rowA in tableA where rowA.int1 == rowC.intC || rowA.int2 == rowC.intC join rowB in tableB on rowA.intB equals rowB.intB select new { rowA, rowB, rowC }; 

这可能会有所帮助。

 var locations = from r1 in (from a in context.A join b in context.B on a.ID equals b.ID select new { a.Prop1, a.Prop2, b.Prop3, b.ID }) join c in context.C on r1.ID equals c.ID select new { r1.Prop1, r2.Prop2, r2.Prop3, c.Prop4 }; 

对于我的生活,我无法得到。在我的查询中可以工作(也许这就是我如何使用LinqPad)但我能够得到以下工作:

 from s in Stores join a in Areas on s.AreaID equals a.ROWID join r in Regions on a.RegionID equals r.ROWID join e in Employees on 1 equals 1 // <-- produces a cartesian product join t in Titles on e.TitleID equals t.ROWID where e.AreaID == a.ROWID || e.RegionID == r.ROWID // <--filters the data based on OR stmt where s.StoreNum == 469 select new { r.RegionName, a.AreaName, s.StoreNum, s.StoreName, t.JobCode, e.FirstName, e.LastName } 

试试这个:-

 var result= tableA.SelectMany(a => tableB.Where(x => xA == aA || xB == aB), (a, b) => new {a, b});