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(;

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 Query