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

标签 c# linq entity-framework linq-to-entities

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

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 )

我被下面的语法困住了

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

这给了我下面的错误

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

如何对表进行多条件连接。

最佳答案

您不必使用连接语法。在 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();

关于c# - 具有多个 OR 条件的 Linq to Entity Join 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15887223/

相关文章:

c# - 如何强制 quartz.net 作业在完成后重新启动

c# - 使用 Entity Framework 加入 View 时使用 .Include()

c# - 我可以通过传入 lambda 表达式来获取参数来检查通用对象的属性吗?

c# - LINQ 选择一列中具有 Max 且另一列中具有 Distinct 的行

c# - Entity Framework DbEntityEntry> '不包含 Where 的定义

c# - ZeroMQ 性能问题

mysql - 在 MySQL 中的单列中插入新值

c# - Int[].Contains 在 EF6 中不起作用

c# - 已经有一个与此命令关联的打开的 DataReader,必须先将其关闭

c# - 重复单步调试≠运行调试