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

3

12

I am using Entity Framework, and I have a line of code that is taking a var and translating it back to an iint for the database.

var record = context.enrollments.SingleOrDefault
  (row => row.userId == int.Parse(UserID) && row.classId == int.Parse(ClassID));

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 this as well

 var record = context.enrollments.FirstOrDefault
  (row => row.userId == Convert.ToInt32(UserID) 
  && row.classId == Convert.ToInt32(ClassID));

and all I receive is this error message, "LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

and finally I have tried this as well, which I know is unusual, but it has worked in the past for similar situations.

var record = context.enrollments.SingleOrDefault
  (row => row.userId == CommonDLL.Sanitize<int>.ConvertType(UserID) 
  && row.classId == CommonDLL.Sanitize<int>.ConvertType(ClassID));

In which I get this error. As you can see I have tried seveal different things and nothing is working, so any help would be great.

Senior answered 22/4, 2014 at 4:42 Comment(4)
If I remember correctly, you should be able to replace int.Parse with Convert.ToInt32 to make it work.Dorian
I should have mentioned this in the comments, but I have tried that and I receive this error message, "{"LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression."}"Senior
Did you try using the full namespace like System.Convert.ToInt32? I don't if this is specifically something that linq doesn't support. Just guessing here.Superintendent
Actually, there is already a question regarding this, with an answer: #7400102 and here #13887796Superintendent
M
20

in Linq to Entity, you should use the methods in your query which is supported by your provider to convert them to expression tree to run on your Data Base side.

all providers must support some methods by default called Canonical Functions (Read More Here), and also you can define your user defined function and stored procedure as edm functions to use in linq query (Read More Here) and (Here).

in addition you can use methods which is supported by providers and can be converted to expression tree which are in EntityFunctions and SqlFunctions.

and finally about your question, you can convert UserID and ClassID before your query, like this:

var UID = int.Parse(UserID);
var CID = int.Parse(ClassID);
var record = context.enrollments.SingleOrDefault
    (row => row.userId == UID && row.classId == CID);
Marleenmarlen answered 24/4, 2014 at 16:6 Comment(1)
Thank you Amir. I was stuck on this one quite a while, and couldn't fins an answer anywhere else.Senior
F
1

This works for me:

  var iId = int.Parse(TextBox1.Text);
 var item = db.Items.Where(p => p.ItemID == iId ).FirstOrDefault();

To make that work you should convert your textbox value outside of the lambda expression.

Foresail answered 12/8, 2016 at 5:29 Comment(0)
P
-2

You can use AsEnumerable() as follows:

var record = context.enrollments.AsEnumerable().FirstOrDefault(row => row.userId == Convert.ToInt32(UserID) && row.classId == Convert.ToInt32(ClassID));
Pathology answered 24/9, 2020 at 11:56 Comment(2)
There is already valid answer, and yours, with invalid code, is not working as you can see in others comment?Hoahoactzin
Sorry, very bad solution, context.enrollments.AsEnumerable() pulls all data into memory and only then selects just one. Be careful with this AsEnumerable() "fix".Tribal

© 2022 - 2024 — McMap. All rights reserved.