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

前端之家收集整理的这篇文章主要介绍了c# – 具有多个OR条件的Linq to Entity Join表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要编写一个可以获得以下SQL查询Linq-Entity状态
  1. SELECT RR.OrderId
  2. FROM dbo.TableOne RR
  3. JOIN dbo.TableTwo M ON RR.OrderedProductId = M.ProductID OR RR.SoldProductId= M.ProductID
  4. WHERE RR.StatusID IN ( 1,4,5,6,7 )

我被卡住了下面的语法

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

这给我下面的错误

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

如何为表执行多条件连接.

解决方法

您不必使用连接语法.在where子句中添加谓词具有相同的效果,您可以添加更多条件:
  1. var query = (from RR in context.TableOne
  2. from M in context.TableTwo
  3. where RR.OrderedProductId == M.ProductID
  4. || RR.SoldProductId == M.ProductID // Your join
  5. where RR.CustomerID == CustomerID
  6. && statusIds.Any(x => x.Equals(RR.StatusID.Value))
  7. select RR.OrderId).ToArray();

猜你在找的C#相关文章