I am trying to Query Database Context using Linq to Entities and I am getting this error:
LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression.`
Code:
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();
}
I tried like this after seeing this
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();
}
But it throws an error:
"The name 'o' does not exist in the current context"
This is my code for the class GetCourseName
namespace IronwoodWeb
{
public class CourseNames
{
public int CourseID { get; set; }
public string CourseName { get; set; }
}
}
int.Parse(o.CourseID)
; the error is because EF didn't know how to writeConvert.ToInt32
in terms of SQL, but I think it should understandint.Parse
.. (best would be to use the appropriate column type and avoid having to deal with this conversion :) – Orlinao
is out of scope). Go back the first query, but replaceConvert.ToInt32
withint.Parse
, hmm nevermind, it is the other way. Seems to be IEnumerable vs. IQueryable difference? – Orlina