我有以下Transact-Sql,我试图转换为LINQ ...并挣扎.
SELECT * FROM Project WHERE Project.ProjectId IN (SELECT ProjectId FROM ProjectMember Where MemberId = 'a45bd16d-9be0-421b-b5bf-143d334c8155')
任何帮助将不胜感激......如果可能的话,我想用Lambda表达式来做.
提前致谢!
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 );
在这种情况下,您可以使用.Contains(),如下所示:
var projects = from p in db.Projects where db.ProjectMembers.Where(m => m.MemberId == "a45bd16d-9be0-421b-b5bf-143d334c8155").Select(pp => pp.ProjectID).Contains(p.ProjectID) select p;