C# linq 使用或条件连接 3 个表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8900077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:04:20  来源:igfitidea点击:

linq join 3 tables with or condition

c#.netsqllinqjoin

提问by Guillermo Varini

I need to create a statement in LINQ with 3 tables and OR condition.

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

My function receives an integer, lets call it intZ. I have 3 tables: tableA, tableBand tableC.

我的函数接收一个整数,让我们调用它intZ。我有 3 个表:tableA,tableBtableC.

tableAhas columns int1, int2and intB. intBis related to tableB.

tableA有列int1int2intBintB与 相关tableB

problem:int1or int2of tableAcan be intZand it has to match with one tableCrecord.

问题:int1or int2oftableA可以intZ并且它必须与一个tableC记录匹配。

I need an OR condition, but I have no idea where to place it. Does it go in the where clause? Or in the equals clause?

我需要一个 OR 条件,但我不知道把它放在哪里。它是否在 where 子句中?还是在等号子句中?

At the moment, I know how to join 3 tables, but the condition is killing me.

目前,我知道如何加入 3 个表,但条件正在杀死我。

What is the difference between the two ways to create statements in linq? Is there a performance impact?

在 linq 中创建语句的两种方式有什么区别?有性能影响吗?

edit: Okay, now I think it's more clear. intZhas to be related with intCfrom tableC, and this number can be int1or int2of tableA.

编辑:好的,现在我觉得它更清楚了。intZ必须与intCfrom相关tableC,并且这个数字可以是int1or int2of tableA

enter image description here

在此处输入图片说明

采纳答案by Magnus

Just add it to a Where. In Linq2Sql this will be translated to an inner join (with or) on tableB

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

from a in tableA
from b in tableB.Where(x => x.A == a.A || x.B == a.B)
select new { a, b };

回答by Jon Skeet

You can't use an "or" condition in joins in LINQ, as it only supports equijoins. But you should be able to do it in a whereclause with no problems. For example:

您不能在 LINQ 的联接中使用“或”条件,因为它仅支持等值联接。但是你应该能够在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 };

回答by Prasanna

This may be helpful.

这可能会有所帮助。

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

回答by K B

For the life of me, I couldn't get the .Where to work in my query (perhaps it's how I'm using LinqPad) but I was able to get the following to work:

对于我的一生,我无法在我的查询中找到 .Where to work(也许这就是我使用 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 }

回答by Mohit Verma

Try this:-

尝试这个:-

var result= tableA.SelectMany(a => tableB.Where(x => x.A == a.A || x.B == a.B), (a, b) => new {a, b});