Linq to Entity Join table with multiple OR conditions

后端 未结 3 1172
死守一世寂寞
死守一世寂寞 2020-12-03 17:49

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

SELECT  RR.OrderId
FROM    dbo.TableOne RR
        JOIN dbo.TableTwo  M ON RR.OrderedPro         


        
相关标签:
3条回答
  • 2020-12-03 18:17

    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();
    
    0 讨论(0)
  • 2020-12-03 18:18

    Change your query syntax from using join to using an additional from clause

      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();
    
    0 讨论(0)
  • 2020-12-03 18:28

    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:

    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();
    
    0 讨论(0)
提交回复
热议问题