获取嵌套实体的计数

时间:2013-01-22 14:03:11

标签: c# linq entity-framework

我正试图在LINQ中计算特定州的员工数量。

我有这样的事情:

States
     |
     Cities
          |
          Posts
              |
              Employees

如何通过选定的Employees计算State次?

我的实体是:

public class Province : EntityBase
{
    public String ProvinceName { get; set; }
    public virtual IList<City> Cities { get; set; }
}

public class City : EntityBase
{
    public String CityName { get; set; }

    public virtual Province Province { get; set; }
    public virtual IList<Post> ElectricPosts { get; set; }
}

public class Post : EntityBase
{
    public String PostName { get; set; }
    public virtual City City { get; set; }
    public virtual IList<Employee> Employees { get; set; }
}

public class Employee : Person
{
    public virtual String FirstName { get; set; }
    public virtual String SureName { get; set; }
    public virtual Post ElectricPost { get; set; }
}

编辑:有趣的是,我可以毫无问题地获得Posts的计数和异常,但是当我想在@HamletHakobyan帖子中尝试时,我会得到{{ 1}}我不知道为什么?

8 个答案:

答案 0 :(得分:11)

我不确定但是试试这个:

var state = ((IObjectContextAdapter)context)
            .ObjectContext
            .CreateObjectSet<Province>("States") as ObjectQuery<Province>;
            // States is entitySetName

int count = state.Cities.Include("ElectricPosts.Employees")
                    .SelectMany(c => c.Posts)
                    .SelectMany(p => p.Employees)
                    .Count();

答案 1 :(得分:4)

您可以使用LINQ to Entities SumCount方法:

int count = db.States
  .Where( state => state.StateId == X )
  .Sum( state => state.Cities
    .Sum( city => city.Posts
      .Sum( post.Employees.Count() )
    )
  )
;

答案 2 :(得分:2)

var q = from emp in dc.Employees
    from post in dc.Posts
    from city in dc.Cities
    from state in dc.States
    where emp.PostID == post.ID
    && post.CityID == city.ID
    && city.StateID == state.ID
    && state.Name == "Tehran"
    select emp;

或只是

var q = from emp in dc.Employees
    where emp.Post.City.State.Name == "Tehran"

q 是该州的员工名单,现在您可以轻松统计他们(我相信您知道如何统计他们)。

答案 3 :(得分:1)

如果我不正确的话,这将成为我解决问题的方法 因为我无法找到一种方法将4 Linq粘在一起,但也许你能够做到这一点

顺便说一下,如果您需要可选的,请查看此link

        var list = new List<Province>();

        string sProvinceName = "";
        string sCityName = "";
        string sPostName = "";
        string sEmployeeFirstName = "";
        string sEmployeeSureName = "";


        var iListListcitys = from province in list
                             where province != null
                             where province.Cities != null
                             where province.ProvinceName == sProvinceName
                             select province.Cities;

        var iListListposts = from icitys in iListListcitys
                             from city in icitys
                             where city != null
                             where city.ElectricPosts != null
                             where city.CityName == sCityName
                             select city.ElectricPosts;

        var iListListEmployees = from posts in iListListposts
                                 from post in posts
                                 where post != null
                                 where post.Employees != null
                                 where post.PostName == sPostName
                                 select post.Employees;

        var listEmployees = from employees in iListListEmployees
                            from employee in employees
                            where employee != null
                            where employee.FirstName == sEmployeeFirstName || employee.SureName == sEmployeeSureName
                            select employee;

        var count = listEmployees.Count();

答案 4 :(得分:1)

我有完全相同的(问题)问题,但遗憾的是我在互联网上找到的解决方案是错误的!所以我试着解决这个问题。 因此,假设我们有一些国家,每个国家都有一些国家,每个州都有一些城市,我们希望得到一些国家视图模型,其中包含每个国家的ID和名称以及所有州和所有城市的数量属于每个国家。解决方案如下:

var varResult =
    OurDatabaseContext.Countries
    .Select(current => new {
        Id = current.Id,
        Name = current.Name,
        StateCount = current.States.Count,
        CityCount = current.States.Count == 0 ? 0 :
        current.States.Select(state =>
          new { CityCount = state.Cities.Count }).Sum(q => q.CityCount)
    };

答案 5 :(得分:0)

db.States.Where(p => p.yourcondition == whatever).Cities.Posts.Employees.Count()

答案 6 :(得分:0)

试试这个:

Province province = ...;
int count = province.Cities.Include(c => c.Posts.Select(p => p.Employees))
    .Where(c => c != null)
    .SelectMany(c => c.Posts)
    .Where(p => p != null)
    .SelectMany(p => p.Employees)
    .Where(e => e != null)
    .Count();

然后逐个删除.Where子句以找到有问题的地方。

您也可以使用join s:

int count = (from c in province.Cities
             join p in db.Posts on c equals p.City
             join e in db.Employees on p equals e.ElectricPost
             select e).Count();

答案 7 :(得分:0)

var cites = (from s in _db.Province where s.ProvinceName == 'name' select s.Cities);
int count = (from p in _db.Posts
         where city.Contains(p.City)
         select p.Employees).Count();