我正在尝试使用实体框架返回.net webapi中具有三列的员工,但返回错误,提示无法将pf匿名类型转换为emp ..我所缺少的内容
public List<EMPLOYEE_DTLS> GetLoginInfo(string UserId)
{
var result = (from emp in db.EMPLOYEE_DTLS
where emp.UserId == UserId
select new
{
emp.FullName,
emp.Role,
emp.Designation
}).ToList();
return result;
}
答案 0 :(得分:0)
您的方法返回List<EMPLOYEE_DTLS>
,但是您正在尝试返回匿名类型的列表。
假设您的EMPLOYEE_DTLS
类具有属性FullName
,Role
和Designation
,请更改您选择的类型:
public List<EMPLOYEE_DTLS> GetLoginInfo(string UserId)
{
var result = (from emp in db.EMPLOYEE_DTLS
where emp.UserId == UserId
select new EMPLOYEE_DTLS
{
FullName = emp.FullName,
Role = emp.Role,
Designation = emp.Designation
}).ToList();
return result;
}
答案 1 :(得分:0)
您应该使用DTO返回列表。
public class EmployeeDTO
{
public string FullName {get; set;}
public string Role {get; set;}
public string Designation {get; set;}
}
public List<EmployeeDTO> GetLoginInfo(string UserId)
{
var result = (from emp in db.EMPLOYEE_DTLS
where emp.UserId == UserId
select new EmployeeDTO
{
FullName = emp.FullName,
Role = emp.Role,
Designation = emp.Designation
}).ToList();
return result;
}