1

我有一个 SQL 查询,我正试图将其翻译成 Lambda,但是and子句让我难过。我没有运气研究如何在 lambda 上执行此操作子句。你们将如何使用 lambda 来制作这个 sql 语句?

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

1 回答 1

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....
   )
于 2013-07-23T04:52:46.697 回答