gpt4 book ai didi

c# - mvc 上的 Linq 查询错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:29:28 24 4
gpt4 key购买 nike

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” on the line Employee = emp

最佳答案

所以它很简单,就像错误所说的那样,您的模型不包含 Employee 的定义,因此您应该这样写:

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

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

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

然后这样写你的查询:

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

关于c# - mvc 上的 Linq 查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32788245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com