小编典典

如何使用LAMBDA表达式在LINQ中进行IN或CONTAINS处理?

sql

我有以下Transact-Sql,我正尝试将其转换为LINQ …并正在挣扎。

SELECT * FROM Project
WHERE Project.ProjectId IN (SELECT ProjectId FROM ProjectMember Where MemberId = 'a45bd16d-9be0-421b-b5bf-143d334c8155')

任何帮助将不胜感激…如果可能的话,我想使用Lambda表达式来做。

提前致谢!


阅读 260

收藏
2021-05-23

共1个答案

小编典典

GFrizzle击败了我。但是这是一个C#版本

var projectsMemberWorkedOn = from p in Projects
                    join projectMember in ProjectMembers on
                        p.ProjectId equals projectMember.ProjectId
                    where projectMember.MemberId == "a45bd16d-9be0-421b-b5bf-143d334c8155"
                    select p;

作为奖励,还有纯LINQ方法链版本:

var projectsMemberWorkedOn =
            Projects.Join( ProjectMembers, p => p.ProjectId, projectMember => projectMember.ProjectId,
                ( p, projectMember ) => new { p, projectMember } )
                .Where( @t => @t.projectMember.MemberId == "a45bd16d-9be0-421b-b5bf-143d334c8155" )
                .Select(@t => @t.p );
2021-05-23