2013-07-23 36 views
1

我有一個SQL查詢,我試圖把它翻譯成Lambda,但是子句讓我難堪。我沒有研究如何在lambda上做這個子句。你們如何使用lambda來做這個sql語句?如何在lambda表達式中實現AND邏輯?

SELECT distinct x.* 
FROM UserInteractions x 
JOIN UserInteractions x2 on x.sourceuser_id = x2.targetuser_id and x.targetuser_id = x2.sourceuser_id 
WHERE x.sourceuser_id = 2 

這是我原來的加盟,但我不知道如何添加「添加」

query = query.Join(db.UserInteractions, 
       x => x.SourceUser, 
       x2 => x2.TargetUser, 
       (x, x2) => new { x, x2 }).Where(f => f.x.SourceUser == user).Select(p => p.x); 

回答

4

使用匿名類型的連接

from x in UserInteractions 
join x2 in UserInteractions 
on new {x.sourceuser_id, x.targetUser_id} equals new {x2.sourceuser_id, x2.targetuser_id} 
select new .... blah blah 

或..

UserInteractions 
    .Join (
     UserInteractions , 
     x => 
     new 
     { 
      x.sourceuser_id, 
      x.targetuser_id 
     }, 
     x2 => 
     new 
     { 
      x2.sourceuser_id, 
      x2.targetuser_id 
     }, 
     (x, x2) => //Whatever it is you want to project out.... 
    ) 
+0

你會如何將它翻譯成lambda? – Furyvore

+0

謝謝,這工作 – Furyvore