if its LINQ to SQL you cant since it tries to render that to real SQL. try to cast the Persons to another object class of your own and then it will be Linq to Object and there it will work. i had this issue a few times, i played with it a little bit until it worked.

BTW i think one of the linq extensions is looking for an IComparable, so worse case you can just implement one


I will give you a one-liner which "could" work, but please provide the exception as well.

var query = _db.Persons.AsEnumerable().OrderBy(p => Decrypt(p.Name));

Your problem is probably, but it is hard to know since we are not given the exception, that you are trying to get your database to execute Decrypt which is not possible. The added ToList() just enumerates, ie fetches the data from database in this case, and then we can order in memory.

You won't get paging to work the way you want since the database cannot execute the query.


You need to actually figure out why IS the paging not working. It's possible to create custom SQL function and let Decrypt work. For that, you need to create SQL function Decrypt that does the work.

This is simple example:

(SQL part)

CREATE FUNCTION ReverseCustName(@string varchar(100))
RETURNS varchar(100)
    DECLARE @custName varchar(100)
    -- Implementation left as exercise for users.
    RETURN @custName

and LINQ part

[Function(Name = "dbo.ReverseCustName", IsComposable = true)]
[return: Parameter(DbType = "VarChar(100)")]
public string ReverseCustName([Parameter(Name = "string",
    DbType = "VarChar(100)")] string @string)
    return ((string)(this.ExecuteMethodCall(this,

See this for more information:

Related Articles