我是 Entity Framework 和 LINQ 的新手 我有兩個專案一個是我的類別庫另一個是我的 UI 專案我正在努力顯示從我的類別庫回傳串列的方法中的員工串列這是我的類:
public static class ViewEmployeesDataManager
{
public static List<Employee> ViewManagerEmployees()
{
using (var context = new HRSystemContext())
{
var query = from empolyee in context.Employees
select new {
Name = empolyee.FullName,
JobTitle = empolyee.JobTitle,
Mobile = empolyee.Mobile};
return query.ToList();
這就是我在表格中的稱呼:
dataGridView1.DataSource=ViewEmployeesDataManager.ViewManagerEmployees();
我收到了這個錯誤
error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string Name, string JobTitle, string Mobile>>' to 'System.Collections.Generic.List<DataAccessLayer.Employee>
任何人都可以幫忙
uj5u.com熱心網友回復:
更新:我通過在我的串列中添加 Object 而不是 Employee 解決了錯誤并且它有效
public static List<Object> ShowingEmployeeInfo()
{
using (var context = new HRSystemContext())
{
var employee = from row in context.Employees
where row.Username == EmployeeDataManager.currentUser
select new {
Id = row.Id,
Name = row.FullName,
Job = row.JobTitle,
Mobile = row.Mobile,
Manager = row.Manager.FullName
};
return employee.ToList<Object>();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426641.html
標籤:C# 实体框架 ef-code-first
