2

Probably I'm blind, but I can't find how to convert string to int. I wrote

query.OrderByDescending(a => SqlFunctions.IsNumeric(
    a.Index.Substring(a.Index.Length - 4, 4)) == 1 ?
    Convert.ToInt32(a.Index.Substring(a.Index.Length - 4, 4)) :
    0);

But, as I suspected, it returns an error

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)'

Moreover SqlFunctions don't have a member Convert. Is any way to achieve this without enumerating the query?

EDIT: int.Parse throws

LINQ to Entities does not recognize the method 'Int32 Parse(System.String)

11
  • 2
    Does int.Parse instead of Convert.ToInt32 work? Commented Apr 22, 2015 at 12:54
  • 3
    @JonSkeet What difference that makes? I'm missing that. Commented Apr 22, 2015 at 12:57
  • 1
    @SriramSakthivel: It was possible that LINQ to Entities knew about int.Parse but not about Convert.ToInt32. That appears not to be the case, but it was at least possible. Commented Apr 22, 2015 at 13:04
  • 2
    @Andrew: You can't cast from string to int. Commented Apr 22, 2015 at 13:04
  • 1
    This issue is a bit frustrating. I ran into it just yesterday afternoon. One thing that you have to keep in mind is that EF has to be able to generate the SQL regardless of the provider. For example, the generated SQL may be different between SQL Server and MySQL. It has to be generic enough that it can handle all situations. Commented Apr 22, 2015 at 13:07

4 Answers 4

4

This is a long shot as I have not tried it myself (I read about it a few weeks ago).

If the SqlFunctions class does not provide a function that you need, you can create a custom function on the SQL Server side and then create a class on the .NET side similar to the SqlFunctions class to call the custom function. There is an attribute that you have to decorate the .NET class with.

Perhaps something like this would work?

You can read more about this here and here.

Sign up to request clarification or add additional context in comments.

2 Comments

this sounds nice, but like a workaround quite common problem. If no one knows any shorter way that will be the answer.
@MarcinJ Note that if you are using code first instead of edm files, you have to do like stackoverflow.com/a/23658354/613130
0

int a=int.Parse(string);

is the way you can convert a string into int in c#

Comments

0

int.TryParse(X.Number, out tmp) ? tmp : 0

Comments

0

Have you tried it with TryParse? something like this:

int number;
query.OrderByDescending(a =>(
                        int.TryParse(a.Item1.Substring(1, 2), 
                                        out number) == true  ? number: 0));

I always use TryParse with lambda expressions.

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
@JNYRanger ok i wanted to comment his post but i realize that i don t have enough reputation. I thought it was one possible solution to his question. I ll ve it in mind for next time.
Sorry, @JNRanger - I think its a reasonable attempt at an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.