0

I have a list which want to order by the ASCII code of one field. What is the equivalent of an ASCII method in LINQ.

revisions = revisions.OrderBy(x => Ascii(x.SubRevision) % 90).ToList();

Method Ascii doesn't exist in LINQ. How I can use it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nnmmss
  • 2,850
  • 7
  • 39
  • 67
  • if `SubRevision` is char you can just add (int) -> `(int)x.SubRevision` otherwise look here: http://stackoverflow.com/questions/5348844/how-to-convert-string-to-ascii – Kamil Budziewski Nov 20 '13 at 07:36
  • 1
    What type of x.SubRevision? And why you have added %90 there? – SergeyS Nov 20 '13 at 07:39

2 Answers2

0

It has nothing to do with LINQ really. If x.SubRevision is a char property you can simply cast it into an integer to get the ASCII value:

revisions = revisions.OrderBy(x => ((int)x.SubRevision) % 90).ToList();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mithrandir
  • 24,869
  • 6
  • 50
  • 66
0

There is, though. The function you need to use is System.Data.Objects.SqlClient.SqlFunctions.Ascii(x.Subrevision). This takes the leftmost character in x.Subrevision and gives the ASCII code for it.

However, this method can only be used in a LINQ to Entities query, not LINQ to SQL.

Like the others have answered, characters in C# can be casted to int for the same effect. So if x.Subrevision is a character you can simply cast it to int, if it's a string, you can cast the leftmost character to string, like this:

(int)x.Subrevision[0]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tobberoth
  • 9,327
  • 2
  • 19
  • 17