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)

Marcin J
  • 378
  • 4
  • 15

4 Answers4

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.

Jason Richmeier
  • 1,595
  • 3
  • 19
  • 38
  • this sounds nice, but like a workaround quite common problem. If no one knows any shorter way that will be the answer. – Marcin J Apr 22 '15 at 13:16
  • 1
    @MarcinJ Note that if you are using code first instead of edm files, you have to do like http://stackoverflow.com/a/23658354/613130 – xanatos Apr 22 '15 at 13:19
0

int a=int.Parse(string);

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

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
xNeyte
  • 612
  • 4
  • 18
0

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

BenM
  • 4,218
  • 2
  • 31
  • 58
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.

BIBD
  • 15,107
  • 25
  • 85
  • 137
paulofer85
  • 555
  • 11
  • 15
  • 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](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – JNYRanger Apr 22 '15 at 14:03
  • @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. – paulofer85 Apr 22 '15 at 14:06
  • 1
    Sorry, @JNRanger - I think its a reasonable attempt at an answer. – BIBD Apr 22 '15 at 19:56