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

发布于 2024-07-10 04:15:38 字数 286 浏览 6 评论 0原文

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

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

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

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归属感 2024-07-17 04:15:39

您可能需要 Any() 运算符:

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

由于 db.ProjectMembers.Where(...) 的结果集始终相同,因此您可以将其拆分,以便它只执行一次:

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

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));
反差帅 2024-07-17 04:15:38

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

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 );
第几種人 2024-07-17 04:15:38

在这种情况下,您可以只使用 .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;

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;
拍不死你 2024-07-17 04:15:38

不需要 lambda,这里只需一个简单的 LINQ 连接:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文