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
Linq to Entity Join table with multiple OR conditions
提问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 where
clause 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 join
to using an additional from
clause
将查询语法从使用更改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();