LINQ预计过滤C#

时间:2012-09-25 15:05:52

标签: c# linq entity-framework

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

以下是原始声明:

return 
    this.ObjectContext.People.
        Include("Careers").
        Include("Careers.Titles").
        Include("Careers.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();

如何在过滤后的查询中包含标题和工资表?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

我会尝试类似的东西,

var query = from p in ObjectContext.People
            join c in ObjectContext.Careers on p equals c.Person
            where c.IsActive
            select p;