我在下面的GetStudentById方法中收到了以下错误消息。 “不能转换system.linq.iqueryable为目标类型system.collections.generic.list”无法将查询结果作为特定类型返回
阙:为什么不能我回到我的结果作为studentDto
的public class StudentRepository : IStudentRepository
{
private TechCollegeEducationEntities db = new TechCollegeEducationEntities();
public List<StudentDto> GetStudentById(string studentId)
{
List<StudentDto> objresult = from c in db.Students
where c.StudentId == 1
select c;
return objresult;
}
public List<StudentDto> GetAllStudents()
{
throw new NotImplementedException();
}
}
下面的列表是我的DTO
public class StudentDto
{
public Int32 StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string Department { get; set; }
}
我只是尝试这样做,现在和它的作品对我..
return (from c in db.Students
select new StudentDto
{
FirstName = c.FirstName,
LastName = c.LastName,
Department = c.Department,
EmailAddress = c.EmailAddress
}).ToList()
请张贴*确切*错误消息。现在看来,Cuong Le的答案是你能想到的最多的。 –
我做到了。不能将system.linq.iqueryable转换为目标类型system.collections.generic.list – user2320476
不知道你用什么来编写C#代码,但微软的C#编译器/ VS提供了更详细的错误,包括类型 - 例如“ ..IQueryable <...> System.Collection.Generic.List ...“ –