mvc上的Linq查询错误

本文关键字:错误 查询 Linq 上的 mvc | 更新日期: 2023-09-27 18:14:36

public class Employee
{
    [Key]
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public int DesignationID { get; set; }
    public virtual Designation Designation { get; set; }
}

(from emp in dc.Employee 
    select new Employee { Employee = emp , Designation = emp.Designation }).Single();

显示"Models.Employee does not contain a definition for Employee"错误,Employee = emp

mvc上的Linq查询错误

就像错误提示的那样简单,你的模型不包含Employee的定义,所以你应该这样写:

(from emp in dc.Employee 
    select new Employee { Name = emp.Name ,...

如果你想要一个雇员对象,你可以像这样创建一个视图模型:

public class EmployeeViewModel
{
    public Employee Employee {get; set;}
    public Designation Designation {get; set;}
}

然后这样写查询:

select new EmployeeViewModel { Employee = emp , Designation = emp.Designation }).Single();