Int.Parse is not supported by LINQ to Entities
-
Saturday, January 22, 2011 2:09 PM
var result = from m in db.BDClassInfo join n in db.BDProfessionInfo on m.ProfessionInfoKey equals n.ProfessionInfoKey join o in db.BDInstitute on n.InstituteKey equals o.InstituteKey where o.InstituteKey == instituteKey && int.Parse(m.EntranceYear) + n.LearnLen > int.Parse(currentTerm.TermNo.Substring(0, 4)) select new { m, n.ProfessionInfoName, o.InstituteName };
I know that LINQ to Entities does not support Int.Parse function, I want to know is there any solution to solve the problem in other way?
Like the problem above, behind the "where", the function Int.Parse is necessary.
- Moved by Sheng Jiang 蒋晟MVP Saturday, January 22, 2011 4:14 PM (From:ADO.NET 与 LINQ)
All Replies
-
Saturday, January 22, 2011 2:23 PM。。。搞了半天发到中文论坛上来了,我还以为是英文论坛呢
-
Saturday, January 22, 2011 4:14 PMMoved to English forums as requested
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Wednesday, January 26, 2011 8:20 AMModerator
Hello a4712635,
Welcome to the MSDN Forum.
I think you are right. By testing, I found that fi you use int.Parse, then you will got an exception because Method 'Int32 Parse(System.String)' has no supported translation to SQL.
However, we can use Convert.ToInt32(). It works fine in my programe. In your case, we can modify it to the following:
Convert.ToInt32(m.EntranceYear) + n.LearnLen > Convert.ToInt32(currentTerm.TermNo.Substring(0, 4))
Actually, i also found the difference between Convert.ToInt32() and int.Parse(). Here they are:
If you've got a string, and you expect it to always be an integer (say, if some web service is handing you an integer in string format), you'd use Int32.Parse(). If you're collecting input from a user, you'd generally user Int32.TryParse(), since it allows you more fine-grained control over the situation when the user enters in invalid input.
Convert.ToInt32() takes an object as its argument, and I believe it invokes Int32.TryParse() when it finds that the object taken as the argument is a string. Convert.ToInt32 also does not throw ArgumentNullException when it's argument is null the way Int32.Parse() does. That also means that Convert.ToInt32() is probably a wee bit slower than Int32.Parse() because it has to ask its argument what it's type is.
If you have any questions please feel free to let us know.
Have a nice day,
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

- Proposed As Answer by Jackie-SunModerator Wednesday, January 26, 2011 8:20 AM
- Marked As Answer by Jackie-SunModerator Monday, January 31, 2011 2:45 AM
-
Thursday, February 17, 2011 5:45 AMThanks very much!
-
Thursday, February 17, 2011 6:00 AMModeratorIt's my pleasure~ :)
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Thursday, February 17, 2011 6:51 AM
I tried Convert.ToInt32() today, but there is still an exception :
Unhandled Exception: System.NotSupportedException: LINQ to Entities does not rec
ognize the method 'Int32 ToInt32(System.String)' method, and this method cannot
be translated into a store expression.my code:
var result = from m in db.BDClassInfo join n in db.BDProfessionInfo on m.ProfessionInfoKey equals n.ProfessionInfoKey join o in db.BDInstitute on n.InstituteKey equals o.InstituteKey where o.InstituteKey == "01" && Convert.ToInt32(m.EntranceYear) + n.LearnLen > Convert.ToInt32(currentterm.Substring(0, 4)) select new { m, n.ProfessionInfoName, o.InstituteName };
-
Thursday, February 17, 2011 7:02 AMModerator
How about modifing your code like this:
var result = from m in db.BDClassInfo.AsEnumerable() ?
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Thursday, February 17, 2011 7:27 AM
Thank you, AsEnumerable() is OK
What does AsEnumerable() mean?
-
Thursday, February 17, 2011 7:33 AMModerator
:) You are welcome.
Please see this.
http://msdn.microsoft.com/en-us/library/bb335435.aspx
And in this topic contains a list of CLR methods that can be converted to command tree canonical functions and executed on the server:
http://msdn.microsoft.com/en-us/library/bb738681.aspx
For CLR methods not on this list, you would have to pull the results down to the client using .AsEnumerable() and execute a LINQ to Objects query.
Thanks,
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


