我有一个叫做员工的模型
[Table("tblemployee")]
public class Employee
{
public int EmpID;
public string EmpName;
[NotMapped]
public string EmpRole;
}
我有另一个角色模型
[Table("tblrole")]
public class Role
{
public int EmpID;
public string RoleName;
}
现在我想要一个employee的最终对象,它将Role类的RoleName设置为Employee类的EmpRole。
var result = from e in Context.Employee
join r in Context.Role on e.EmpID equals r.EmpID
select new Employee {
EmpID = e.EmpID,
EmpName = e.EmpName,
EmpRole = r.RoleName
};
在这里,我不想设置员工类的所有属性。我只想设置来自角色类的emprole属性。还有其他方法可以让我一次选择完整的员工对象并且只设置EmpRole字段吗?请帮忙。
提前致谢
答案 0 :(得分:2)
解决此问题的唯一方法是在表中选择Employee和role,然后应用ToList,然后在列表中应用foreach以在Employee对象中设置EmpRole,然后从列表中选择Employee对象。
var result = (from e in Context.Employee
join r in Context.Role on e.EmpID equals r.EmpID
select new { Employee = e, Role = r}).ToList();
result.ForEach(d => {d.Employee.EmpRole = d.Role.RoleName;});
var finalResult = result.Select(d => d.Employee);