2

I want to sort a (JamMulai) that are string but the value are numbers. I'd like (JamMulai) to sort numerically

my query:

var query = from j in db.JadwalKuliah
            orderby j.JamMulai
            select j

and j.JamMulai value is like { "13", "12,3", "7", "15", "10,3"};

output:

(10,3), (12,3), 13, 15, 7

I'd like:

7, (10,3), (12,3), 13, 15

I'm trying to convert to double :

var query = from j in db.JadwalKuliah
            orderby Convert.ToDouble(j.JamMulai)
            select j

but the error is:

Additional information: LINQ to Entities does not recognize the method 'Double ToDouble(System.String)' method, and this method cannot be translated into a store expression.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Viedya AP
  • 23
  • 4
  • You are using { "13", "12,3", "7", "15", "10,3"}; as j.JamMulai value is it a typo or the value is like that only because it is having "12,3" and "10,3" which will be treated a string eventhough you convert or Parse – Rajesh Jan 21 '15 at 05:01
  • it's not a typo, the value of JamMulai in table is like that – Viedya AP Jan 21 '15 at 06:26

2 Answers2

2

Use parse instead of convert

    var numbers = new string[]{ "13", "12,3", "7", "15", "10,3"};

    var sortedNumbers = numbers.
        Select(number => double.Parse(number)).
        OrderBy(number => number).ToArray();
Rajesh
  • 1,600
  • 5
  • 33
  • 59
Y.S
  • 1,860
  • 3
  • 17
  • 30
  • it's still not working when i was started the project. that error is: Additional information: LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression. – Viedya AP Jan 21 '15 at 06:39
  • Try looking into this post http://stackoverflow.com/questions/5971521/linq-to-entities-does-not-recognize-the-method-double-parsesystem-string-met – Y.S Jan 21 '15 at 07:11
2

The problem here is that your query is being translated into SQL and run on the database, and Entity Framework doesn't know how to translate Double ToDouble(System.String) into valid SQL code, you can load the data into memory and then perform orderby:-

var query = db.JadwalKuliah.ToList()
              .OrderBy(x => double.Parse(x))
              .ToArray();
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • Hi, how can I orderBy a Double for any language? I'm having problem with english and portuguese languages. – Patrick May 06 '15 at 09:58