C# 具有多个 OR 条件的 Linq to Entity Join 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15887223/
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-10 18:17:46  来源:igfitidea点击:

Linq to Entity Join table with multiple OR conditions

c#linqentity-frameworklinq-to-entities

提问by HaBo

I need to write a Linq-Entity state that can get the below SQL query

我需要编写一个可以获取以下 SQL 查询的 Linq-Entity 状态

SELECT  RR.OrderId
FROM    dbo.TableOne RR
        JOIN dbo.TableTwo  M ON RR.OrderedProductId = M.ProductID OR RR.SoldProductId= M.ProductID
WHERE   RR.StatusID IN ( 1, 4, 5, 6, 7 )

I am stuck with the below syntax

我坚持使用以下语法

 int[] statusIds = new int[] { 1, 4, 5, 6, 7 };
            using (Entities context = new Entities())
            {
                var query = (from RR in context.TableOne
                             join M in context.TableTwo on new { RR.OrderedProductId, RR.SoldProductId} equals new { M.ProductID }
                             where RR.CustomerID == CustomerID 
                             && statusIds.Any(x => x.Equals(RR.StatusID.Value))
                             select RR.OrderId).ToArray();
            }

this gives me below error

这给了我以下错误

Error 50 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.

错误 50 连接子句中的表达式之一的类型不正确。调用“加入”时类型推断失败。

How can I do a Multiple condition join for a table.

如何对表执行多条件联接。

采纳答案by Gert Arnold

You don't have to use the join syntax. Adding the predicates in a whereclause has the same effect and you can add more conditions:

您不必使用连接语法。在where子句中添加谓词具有相同的效果,您可以添加更多条件:

var query = (from RR in context.TableOne
             from M in context.TableTwo 
             where RR.OrderedProductId == M.ProductID
                   || RR.SoldProductId == M.ProductID // Your join
             where RR.CustomerID == CustomerID 
                   && statusIds.Any(x => x.Equals(RR.StatusID.Value))
             select RR.OrderId).ToArray();

回答by Aducci

Change your query syntax from using jointo using an additional fromclause

将查询语法从使用更改join为使用附加from子句

  var query = (from RR in context.TableOne
               from M in context.TableTwo.Where(x => x.ProductID == RR.OrderedProductId || x.ProductID == RR.SoldProductId)
               where statusIds.Any(x => x.Equals(RR.StatusID.Value))
               select RR.OrderId).ToArray();

回答by Nowshath

Multiple Joins :

多个连接:

var query = (from RR in context.TableOne
             join M in context.TableTwo on new { oId = RR.OrderedProductId,  sId = RR.SoldProductId} equals new { oId = M.ProductID, sId = M.ProductID }
             where RR.CustomerID == CustomerID 
             && statusIds.Any(x => x.Equals(RR.StatusID.Value))
             select RR.OrderId).ToArray();