Linq Subquery Where子句

时间:2010-08-25 23:55:10

标签: linq filter subquery where-clause

我需要一些关于lini查询的帮助。它应该相当简单,但它正在踢我的屁股。

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

子查询本身就是这样。

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

但这会导致错误。任何帮助解决这个难题的人都会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

然后在主查询中使用它。

相关问题