将带有AND子句的JOIN转换为Lambda

时间:2013-07-20 20:53:24

标签: c# entity-framework

我在RAW sql中有这个查询:

SELECT DISTINCT b.SourceUser_Id
FROM UserInteractions b
JOIN UserInteractions x on x.SourceUser_Id=1 and x.TargetUser_Id=b.SourceUser_Id

UserInteractions是一个连接User表与自身的多对多关系的表(用于创建友谊关系)。

此查询返回所有相互作用的UserInteractions(I.E:它只返回目标已经回馈过你的UserInteractions。

sql查询工作正常,但我无法弄清楚如何将其转换为EntityFramework的lambda。

我需要一种方法来使用Join()方法。

1 个答案:

答案 0 :(得分:1)

类似的东西:

var s = from b in UserInteractions 
             join x in UserInteractions on b.TargetUser_Id equals x.TargetUser_Id
             where x.SourceUser_Id == 1
                         select b.SourceUser_Id;

LAMBDA:

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