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

本文关键字:实现 AND 逻辑 表达式 lambda | 更新日期: 2023-09-27 18:24:16

我有一个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);

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

为加入使用匿名类型

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....
   )