1

I am using Entity Framework, and I have a line of code that convert string field (id)to int and compare with a number

 students = students.Where(s => (Int32.Parse( s.id)>5555));

Whenever I try to run it I receive rhis error. "LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression."

I have tried seveal different things and nothing is working, so any help would be great.

  • 1
    Did you try `Convert.ToInt32` ? – Dhrumil May 09 '15 at 05:16
  • yes.it didnt work.and caused that error – ehsan arzankar May 09 '15 at 05:23
  • Try this `Int32.TryParse` – Pradnya Bolli May 09 '15 at 05:24
  • If your `model.id` have db type `int`, why you even need to to parse it to int in linq2entities? I think you should repair your model and not cast at all – Jan 'splite' K. May 09 '15 at 05:26
  • 2
    possible duplicate of [Entity Framework/Linq EXpression converting from string to int](http://stackoverflow.com/questions/16694716/entity-framework-linq-expression-converting-from-string-to-int) – Alex May 09 '15 at 05:27
  • Possible duplicate of [LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression](http://stackoverflow.com/questions/23210526/linq-to-entities-does-not-recognize-the-method-int32-parsesystem-string-meth) – wkl Apr 12 '17 at 12:43

1 Answers1

5

Firstly I would highly recommend against converting the column into an int, you lose the indexing on the column. You rather convert the int into a string. However here is how to fix your code.

  1. Firstly sign the Contributor License Agreement.
  2. Then you fork the Entity Framework git repo.
  3. Write a new MethodCallTranslator.CallTranslator which will take Convert.ToInt32(string) and replace it with (int) string.
  4. Register the new MethodCallTranslator.CallTranslator within MethodCallTranslator.
  5. Write unit tests for your test case.
  6. Check in
  7. Create Pull Request
  8. Wait
  9. Download new version of Entity Framework from nuget
Aron
  • 15,464
  • 3
  • 31
  • 64