17

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; }
    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
user1905332
  • 173
  • 1
  • 1
  • 4
  • 2
    Try `int.Parse(o.CourseID)`; the error is because EF didn't know how to write `Convert.ToInt32` in terms of SQL, but I think it should understand `int.Parse` .. (best would be to use the *appropriate column type* and avoid having to deal with this conversion :) –  Dec 14 '12 at 22:50
  • @pst using int.parse throws the same error "The name 'o' does not exist in the current context" and I can't use the same column type, it also shows error " Cannot implicitly convert type....." Anyway thanks for trying to help me! – user1905332 Dec 14 '12 at 23:00
  • 2
    Another way to get around this issue (where EF cannot translate some expression into SQL) is to run a simpler query first and at the end of it force it to populate via .ToList(). Once you have it in memory, you are no longer bound by having to convert your expressions to SQL. – Heather Dec 14 '12 at 23:02
  • @user1905332 No, that's a *different* issue (`o` is out of scope). Go back the *first* query, but replace `Convert.ToInt32` with `int.Parse`, hmm [nevermind](http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/6c8892db-8df7-4174-b9b0-764dc1df82ad), it is the other way. Seems to be IEnumerable vs. IQueryable difference? –  Dec 14 '12 at 23:08
  • @pst No worries, the answer lazyberezovsky posted works, anyways thanks for trying to help me! – user1905332 Dec 14 '12 at 23:14
  • @Heather solved the problem, anyways thanks for trying to help me! – user1905332 Dec 14 '12 at 23:16

4 Answers4

39
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
                  });
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
4

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.

bubi
  • 6,414
  • 3
  • 28
  • 45
3

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.

Matthew
  • 9,851
  • 4
  • 46
  • 77
-3

Explicit conversion is simple and works: (int)o.CourseID

var course = from o in entities.UniversityCourses
                select new CourseNames
                {
                    CourseID = (int)o.CourseID,
                    CourseName = o.CourseName,
                 };