我正在尝试使用 Linq to Entities 查询数据库上下文,但出现此错误:
LINQ to Entities 无法识别方法“Int32 Int32(System.String)”方法,并且该方法无法转换为存储表达式。
代码:
public IEnumerable<CourseNames> GetCourseName()
{
var course = from o in entities.UniversityCourses
select new CourseNames
{
CourseID = Convert.ToInt32(o.CourseID),
CourseName = o.CourseName,
};
return course.ToList();
}
public IEnumerable<CourseNames> GetCourseName()
{
var temp = Convert.ToInt32(o.CourseID);
var course = from o in entities.UniversityCourses
select new CourseNames
{
CourseID = temp,
CourseName = o.CourseName,
};
return course.ToList();
}
但它会抛出一个错误:
“当前上下文中不存在名称‘o’”
这是我的课程代码GetCourseName
namespace IronwoodWeb
{
public class CourseNames
{
public int CourseID { get; set; }
public string CourseName { get; set; }
}
}