如何在lambda表达式中实现AND逻辑?

时间:2013-07-23 04:41:20

标签: c# entity-framework lambda

我有一个SQL查询,我正在尝试将其转换为Lambda,但子句让我感到难过。我没有运气研究如何在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);

1 个答案:

答案 0 :(得分: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....
   )