我想製作一個簡單的記錄管理應用程序。 我想獲得每個部門的總員工數。到目前爲止,我有這個。按計數ASP.NET MVC
return Context.Employees
.GroupBy(e => e.DepartmentID)
.Select(x => new { Employee = x.Count() });
但它給了我一個紅色的擺動線,說我可能會缺少演員。
無法隱式轉換類型System.Linq.IQueryable < AnonymousType#>到System.Collections.Generic.IEnumerable <SimpleRMA.Models.Department>。一個顯式轉換存在(是否缺少強制轉換?)
這是我想要的結果:
DepartmentName | Employees
Department1 | 4
Department2 | 6
我應該怎麼做,在lambda表達式?由於
系類:
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
public string Location{ get; set; }
public virtual List<Employee> Employees { get; set; }
}
Employee類:
public class Employee
{
public int EmployeeID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public DateTime Birthday { get; set; }
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }
}
你的方法的返回類型是什麼? –
什麼是*確切*錯誤信息? –
'不能將類型System.Linq.IQueryable隱式轉換爲System.Collections.Generic.IEnumerable 。存在明確的轉換(您是否缺少演員?)'@JonSkeet –
Cyval