LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression

public IEnumerable<CourseNames> GetCourseName()
{
    var courses = from o in entities.UniversityCourses
                  select new { o.CourseID, o.CourseName };

    return courses.ToList() // now we have in-memory query
                  .Select(c => new CourseNames()
                  {
                     CourseID = Convert.ToInt32(c.CourseID), // OK
                     CourseName = c.CourseName
                  });
}

If you dont want to materialize the query (retrieve the data) you can use cast (i.e. (int) o.CourseId). Is converted to SQL CAST AS statement.


You could also bring back the value as a string (as it is apparently stored) and then Convert it after.

The error on 'o' being out of the context is that you are only declaring o in the Linq query and it can only be referenced in that scope.