1

我想根据包含的表过滤我的 LINQ 查询,但遇到了一些麻烦。

这是原始声明,它有效:

return 
    this.ObjectContext.People.
        Include("Careers").
        Include("Careers.Titles").
        Include("Careers.Titles.Salaries");

现在我正在尝试使用投影过滤来过滤职业,但遇到了麻烦。它编译但它遗漏了 Titles 和 Salaries 表,这会导致运行时错误,我似乎无法将这些表重新添加:

var query1 = (
    from c in 
    this.ObjectContext.People.
        Include("Careers").
        Include("Careers.Titles").
        Include("Careers.Titles.Salaries")
    select new
    {
        c,
        Careers = from Careers in c.Careers
                  where Careers.IsActive == true
                  select Careers
    });

    var query = query1.AsEnumerable().Select(m => m.c);
    return query.AsQueryable();

如何在过滤后的查询中包含职称和薪水表?

4

2 回答 2

3

您可以大大简化查询,这应该可以解决您的问题。我假设您希望所有人都拥有至少 1 个活跃的职业:

var query = 
    from c in 
    this.ObjectContext.People.
        Include("Careers").
        Include("Careers.Titles").
        Include("Careers.Titles.Salaries")
    where c.Careers.Any(c => c.IsActive);

return query;
于 2012-09-25T15:11:34.337 回答
0

我会尝试类似的东西,

var query = from p in ObjectContext.People
            join c in ObjectContext.Careers on p equals c.Person
            where c.IsActive
            select p;
于 2012-09-25T15:37:59.177 回答