Skip to main content
 首页 » 编程设计

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

2024年02月13日13jillzhang

我有一个 SQL 查询,我试图将其转换为 Lambda,但是 and 子句让我很困惑。我没有研究如何在 lambda 上执行此 and 子句。你们如何使用 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); 

请您参考如下方法:

为连接使用匿名类型

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