Linq 子查询Where 子句

发布于 2024-09-16 04:49:23 字数 1534 浏览 7 评论 0原文

我需要一些有关 linq 查询的帮助。它应该相当简单,但它却让我大吃一惊。

我需要使用子查询来过滤掉主查询中的数据,但我尝试使用的每个路径都会失败。

子查询本身看起来像这样。

int pk = (from c in context.PtApprovedCertifications
         where c.FkosParticipant == 112118 &&
         (!excludedActionTypes.Contains(c.FkMLSosCodeActionType)) &&
         c.EffectiveDate <= DateTime.Now &&
         c.FkptApprovedCertificationVoidedBy == null
         orderby c.EffectiveDate descending,c.PK descending
         select c.PK).FirstOrDefault();

这按预期工作,但正如您所看到的,我插入了数字 112118。这应该是主查询的主键。

我一直在处理的组合查询如下所示。

IQueryable<PtAMember> result = (from p in context.PtAMembers
    where (p.FkptACertification == (from c in context.PtApprovedCertifications
        where c.FkosParticipant == p.PtApprovedCertification.OsParticipant.PK &&
        (!excludedActionTypes.Contains(c.FkMLSosCodeActionType)) &&
        c.EffectiveDate <= DateTime.Now &&
        c.FkptApprovedCertificationVoidedBy == null
        orderby c.EffectiveDate descending, c.PK descending
        select c.PK).FirstOrDefault()) &&
            (p.LastName.ToLower().Contains(param.ToLower()) ||
            p.FirstName.ToLower().Contains(param.ToLower()) ||
            p.SocialSecurityNumber.Contains(param))
    select p).Distinct().OrderBy(PtAMembers => PtAMembers.LastName).ThenBy(PtAMember => PtAMember.FirstName);

但这会导致错误。任何帮助解决这个难题的帮助将不胜感激。

谢谢!

I need some help with thsi linq query. It shoudl be fairly simple, but it is kicking my butt.

I need to use a subquery to filter out data from the main query, but every path I have tried to use results in failure.

The subquery by itself looks like this.

int pk = (from c in context.PtApprovedCertifications
         where c.FkosParticipant == 112118 &&
         (!excludedActionTypes.Contains(c.FkMLSosCodeActionType)) &&
         c.EffectiveDate <= DateTime.Now &&
         c.FkptApprovedCertificationVoidedBy == null
         orderby c.EffectiveDate descending,c.PK descending
         select c.PK).FirstOrDefault();

This works as expected but as you can see I plugged in the number 112118. This should be the primary key from main query.

The combined query I've been working on looks like this.

IQueryable<PtAMember> result = (from p in context.PtAMembers
    where (p.FkptACertification == (from c in context.PtApprovedCertifications
        where c.FkosParticipant == p.PtApprovedCertification.OsParticipant.PK &&
        (!excludedActionTypes.Contains(c.FkMLSosCodeActionType)) &&
        c.EffectiveDate <= DateTime.Now &&
        c.FkptApprovedCertificationVoidedBy == null
        orderby c.EffectiveDate descending, c.PK descending
        select c.PK).FirstOrDefault()) &&
            (p.LastName.ToLower().Contains(param.ToLower()) ||
            p.FirstName.ToLower().Contains(param.ToLower()) ||
            p.SocialSecurityNumber.Contains(param))
    select p).Distinct().OrderBy(PtAMembers => PtAMembers.LastName).ThenBy(PtAMember => PtAMember.FirstName);

This results in an error though. Any help in solving this conundrum would greatly appreciated.

Thanks!

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

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

发布评论

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

评论(1

萌吟 2024-09-23 04:49:23

如何将子查询转换为查找函数:

Func<int, int> pkLookup = n => (from c in context.PtApprovedCertifications
                      where c.FkosParticipant == n &&
                      (!excludedActionTypes.Contains(c.FkMLSosCodeActionType)) &&
                      c.EffectiveDate <= DateTime.Now &&
                      c.FkptApprovedCertificationVoidedBy == null
                      orderby c.EffectiveDate descending,c.PK descending
                      select c.PK).FirstOrDefault();

然后在主查询中使用它。

How about turning your subquery into a lookup function:

Func<int, int> pkLookup = n => (from c in context.PtApprovedCertifications
                      where c.FkosParticipant == n &&
                      (!excludedActionTypes.Contains(c.FkMLSosCodeActionType)) &&
                      c.EffectiveDate <= DateTime.Now &&
                      c.FkptApprovedCertificationVoidedBy == null
                      orderby c.EffectiveDate descending,c.PK descending
                      select c.PK).FirstOrDefault();

then using that in your main query.

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