How do you do an IN or CONTAINS in LINQ using LAMB

2019-06-18 22:17发布

I have the following Transact-Sql that I am trying to convert to LINQ ... and struggling.

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

Any help would be greatly appreciated ... I would like to do it with Lambda expressions, if possible.

Thanks in advance!

4条回答
叛逆
2楼-- · 2019-06-18 22:31

GFrizzle beat me to it. But here is a C# version

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;

And as a bonus a purely LINQ method chain version as well:

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 );
查看更多
做个烂人
3楼-- · 2019-06-18 22:35

In this context, you can just use .Contains(), something like this:

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;
查看更多
劫难
4楼-- · 2019-06-18 22:47

No need for a lambda, you've just got a simple LINQ join here:

From p in db.Projects _
Join m in db.ProjectMember On p.ProjectId Equals m.ProjectId _
Where m.MemberId = "a45bd16d-9be0-421b-b5bf-143d334c8155" _
Select p
查看更多
太酷不给撩
5楼-- · 2019-06-18 22:50

You probably want the Any() operator:

var q = db.Projects
          .Where(p => db.ProjectMembers
                        .Where(pm => pm.MemberId == memberId)
                        .Any  (pm => p.ProjectId == pm.ProjectId)); 

Since the result set of db.ProjectMembers.Where(...) will always be the same, you can split it up so that it's executed only once:

var projectMembers = db.ProjectMembers.Where(pm => pm.MemberId == memberId);
var q              = db.Projects
                       .Where(p  => projectMembers
                                    .Any(pm => p.ProjectId == pm.ProjectId));
查看更多
登录 后发表回答