簡體   English   中英

使用實體框架,如何訪問與每個辦公室(另一個實體)相對應的雇員數量(一個實體)?

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

我有兩個實體:一個雇員實體和一個辦公室實體。 這兩個實體之間存在一對多的關系-也就是說,一個辦公室可能有很多員工,但是每個員工只有一個辦公室。

我正在使用Entity Framework 6來處理項目。 我目前正在使用本教程來學習如何使用Entity Framework創建簡單的Web應用程序。 我對代碼進行了一些更改,以便與自己的實體一起玩。

現在,我正在嘗試讓我的應用程序的“關於”頁面顯示每個辦公室的員工人數。 換句話說,我正在嘗試創建一個兩列表,其中第一列包含辦公室的所有位置,第二列包含每個位置的相應雇員數量。 我相信這意味着我必須從每個數據庫(表?)中提取數據,但是我不知道如何獲取每個辦公室的員工人數。 獲取總數很容易。 我可以編寫以下查詢:

NumOfEmployees = locationGroup.Count()

其中locationGroup是將來自兩個實體的數據分組的變量,而NumOfEmployees是LocationGroup類中的公共int。 這是我當前正在使用的代碼塊(其中大部分來自教程-我已評論了添加的部分)。

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

從代碼中可以看到,

NumOfEmployees = locationGroup.Count()

不做我想做的事。 我希望它返回每個辦公地點的員工人數,但相反,它只是返回員工總數並將其寫在每個辦公地點。 在我的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; }
}

因此,我試圖訪問該OfficeID並使用條件語句,以便它僅計算與之對應的OfficeID的那些雇員。 我一直在尋找解決方案,我無法告訴我是否可以通過特定的匯總查詢在同一行中執行此操作,或者是否需要創建一個循環來統計每個辦公室的員工人數。 問題是我不知道訪問每個辦公室擁有的每個員工人數的語法。 我什至不知道如何在select子句中訪問辦公室+員工的屬性

select new AddressGroup()

在上面的About()方法中,也不需要。 上面About()方法的任何幫助將不勝感激。

編輯:我在下面添加了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; }
}

這是數據庫上最簡單的方法,在那里進行分組和計數:

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

然后創建您的班級OfficeEmployeeCount:

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

並以您的方式訪問它,如下所示:

@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>

或直接從您的控制器喂辦公室:

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

然后在您的視圖中,您可以像這樣訪問:

@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>

第一種方法在數據庫上最簡單,特別是如果您有大量員工。 第二種方法在視圖中更加靈活,例如,如果您要隨后列出實際雇員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM