May 18, 2011

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

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

I got the above error in the code bellow.
   
                Student_Marks e = (from e1 in DBobj.Student_Marks
                where e1.Student_Id == Convert.ToInt32(model.Student_Id)
                select e1).First();
                e.Marks = int.Parse(model.Marks);
                DBobj.SaveChanges();


 Note: model.Student_Id is string in the above code           

 If you use Convert.ToInt32(“ID”) in the Where clause the upper Exception is thrown.
Solution is to declare a variable of type int, which contains the converted value and use this variable in the Where query.


So I changed the code in the below manner and it worked.


         int StudentId = Convert.ToInt32(model.Student_Id);
         Student_Marks e = (from e1 in DBobj.Student_Marks
         where e1.Student_Id == StudentId
         select e1).First();
         e.Marks = int.Parse(model.Marks);
         DBobj.SaveChanges();

3 comments:

  1. Very usefull! Thanks!

    ReplyDelete
  2. You save me a lot of headache. This was pissing me off. I had to use SQL to Linq but with your help, I revert back to Linq to Entities. Thanks a million...

    ReplyDelete