简体   繁体   English

使用实体框架,如何访问与每个办公室(另一个实体)相对应的雇员数量(一个实体)?

[英]Using Entity Framework, how do I access the number of employees (one entity) corresponding to each office (another entity)?

I have two entities: an employee entity and an office entity. 我有两个实体:一个雇员实体和一个办公室实体。 There is a one-many relationship between these two entities -- that is, one office may have many employees but each employee only has one office. 这两个实体之间存在一对多的关系-也就是说,一个办公室可能有很多员工,但是每个员工只有一个办公室。

I am using Entity Framework 6 to work on a project. 我正在使用Entity Framework 6来处理项目。 I am currently using this tutorial to learn how to create a simple web application using Entity Framework. 我目前正在使用本教程来学习如何使用Entity Framework创建简单的Web应用程序。 I have changed the code a bit to play around with my own entities. 我对代码进行了一些更改,以便与自己的实体一起玩。

Right now I am trying to have the 'about' page of my application display the number of employees that are at each office. 现在,我正在尝试让我的应用程序的“关于”页面显示每个办公室的员工人数。 In other words, I'm trying to create a two-column table where the first column contains all the locations of the offices, and the second column contains the corresponding number of employees at each location. 换句话说,我正在尝试创建一个两列表,其中第一列包含办公室的所有位置,第二列包含每个位置的相应雇员数量。 I believe this means I must pull data from each database (table?), however I don't know how to get the number of employees for each office. 我相信这意味着我必须从每个数据库(表?)中提取数据,但是我不知道如何获取每个办公室的员工人数。 Getting the total number is easy. 获取总数很容易。 I can just write the following query: 我可以编写以下查询:

NumOfEmployees = locationGroup.Count()

where locationGroup is the variable grouping the data from the two entities, and NumOfEmployees is a public int from the LocationGroup class. 其中locationGroup是将来自两个实体的数据分组的变量,而NumOfEmployees是LocationGroup类中的公共int。 Here is the block of code I am currently using (most of it coming from the tutorial -- I have commented the parts I added). 这是我当前正在使用的代码块(其中大部分来自教程-我已评论了添加的部分)。

public ActionResult About()
{
    IQueryable<LocationGroup> data = from employee in db.Employees
            group employee by employee.FirstName into locationGroup

            //added the second pull of data from the other db
                                    from office in db.Offices
            group office by office.Location into locationGroup
            select new LocationGroup()
            {
                Location = locationGroup.Key,
                NumOfEmployees = locationGroup.Count() //modified this from original
            };
    return View(data.ToList());
}

As you can see from the code, the 从代码中可以看到,

NumOfEmployees = locationGroup.Count()

doesn't do what I want it to do. 不做我想做的事。 I want it to return the number of employees for each office location, but instead it just returns the total number of employees and writes that for each office location. 我希望它返回每个办公地点的员工人数,但相反,它只是返回员工总数并将其写在每个办公地点。 In my Employee.cs class, the Employee entity has an OfficeID to which it can refer: 在我的Employee.cs类中,Employee实体具有一个OfficeID,它可以引用:

public class Employee
{
    public int EmployeeID { get; set; }
    public int OfficeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int PhoneNum { get; set; }
    public DateTime DoB { get; set; }
    public DateTime HireDate { get; set; }
    public string JobTitle { get; set; }
    public int Salary { get; set; }

    public virtual Office Office { get; set; }
}

So I was trying to access that OfficeID and use a conditional statement so it only counts those employees for which the corresponding OfficeID. 因此,我试图访问该OfficeID并使用条件语句,以便它仅计算与之对应的OfficeID的那些雇员。 I have looked around for a solution and I can't tell if I can do this in one line with a particular aggregate query, or if I need to create a loop that counts the employees for each office. 我一直在寻找解决方案,我无法告诉我是否可以通过特定的汇总查询在同一行中执行此操作,或者是否需要创建一个循环来统计每个办公室的员工人数。 The problem is I don't know the syntax for accessing the number of each employees that each office has. 问题是我不知道访问每个办公室拥有的每个员工人数的语法。 I don't even know how I would access the properties of the offices + employees inside the select clause 我什至不知道如何在select子句中访问办公室+员工的属性

select new AddressGroup()

in the About() method above, nor if I need to. 在上面的About()方法中,也不需要。 Any help with the About() method above would be much appreciated. 上面About()方法的任何帮助将不胜感激。

Edit: I have added the Office class below: 编辑:我在下面添加了Office类:

public class Office
{
    public int ID { get; set; }
    public string Location { get; set; }
    public string BusinessName { get; set; }

    public virtual ICollection<Employee> Employees { get; set; }
}

Here is the easiest way on your database, doing the grouping and counting there: 这是数据库上最简单的方法,在那里进行分组和计数:

var result=db.Offices
  .Select(o=>new OfficeEmployeeCount{
    OfficeName=o.BusinessName,
    EmployeeCount=o.Employees.Count()});

Then create your class OfficeEmployeeCount: 然后创建您的班级OfficeEmployeeCount:

public class OfficeEmployeeCount
{
  public string OfficeName {get;set;}
  public int EmployeeCount {get;set;}
}

And access it in your view like so: 并以您的方式访问它,如下所示:

@model IQueryable<OfficeEmployeeCount>
<table>
<thead><tr><th>Office Name</th><th>Employees</th></tr></thead>
@foreach(var office in Model)
{
  <tr>
    <td>@office.OfficeName</td>
    <td>@office.EmployeeCount</td>
  </tr>
}
</table>

or just feed the offices directly from your controller: 或直接从您的控制器喂办公室:

var result=db.Offices.Include(o=>o.Employees);
return View(result);

Then in your view, you can access in like so: 然后在您的视图中,您可以像这样访问:

@model IQueryable<Office>
<table>
<thead>
 <tr>
  <th>Office Name</th>
  <th>Employee Count</th>
  <th>Employee Names</th>
  <th>Employees</th>
 </tr>
</thead>
@foreach(var office in Model)
{
  <tr>
    <td>@office.BusinessName</td>
    <td>@office.Employees.Count()</td>
    <td>@String.Join(",",office.Employees.Select(e=>e.FirstName + " " + e.LastName))</td>
    <td>@foreach(var e in o.Employees)
    {
      @Html.ActionLink(e.FirstName + " " + e.LastName,"Details","Employees",new {ID=e.ID})
    }
    </td>
  </tr>
}
</table>

The first way is easiest on your database, especially if you have a large number of employees. 第一种方法在数据库上最简单,特别是如果您有大量员工。 The second way is more flexible in the view, such as if you want to then list the actual employees. 第二种方法在视图中更加灵活,例如,如果您要随后列出实际雇员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM