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?
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?
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();
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]