2013-04-08 23 views
9
私は、以下の構文

LINQのエンティティに複数のOR条件でテーブルに参加

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

これをで立ち往生しています

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) 

以下のSQLクエリを取得することができますLINQのエンティティの状態を記述する必要が

以下のエラーが表示されます

エラー50結合句の式の1つが間違っています。 'Join'の呼び出しで型の推論が失敗しました。

表に対して複数条件結合を実行するにはどうすればよいですか。

答えて

21

結合構文を使用する必要はありません。 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(); 
+0

これはうまくいきました。私はSOを探していて、どこにRR.OrderedProductId/RR.SoldProductIdがM.ProductIDと等しいのかなどを見つけましたが、それは自分のコードでは機能しませんでした。 – HaBo

7
する 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(); 
+0

あなたの両方の答えが私のために働いた。申し訳ありませんが、回答は1つしか選択できません。だから私はあなたに投票を与え、回答として@gert Arnoldを選んでいます – HaBo

2

複数のジョイン:

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(); 
関連する問題