views:

43

answers:

1

I am using EF 4 but it is giving me error when I try to order my list.

Unable to cast the type 'System.String' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

This is my code to get the experssion by entering the property name, example below get the Customer Name

       var param = Expression.Parameter(typeof(Customer), "N");

        var sortExpression = Expression.Lambda<Func<T, object>>
            (Expression.Convert(Expression.Property(param, "Name"), typeof(object)), param);

And my EF code is

GetObjectSet<T>().AsQueryable().OrderBy(sortExpression).Skip(0).Take(5);

I know it is some sort of casting problem with EF because it works without the EF but it gives me this error when i hook it up with EF. Is there a work around or something because i don't want to use LINQ.

A: 

I believe this answer addresses what you're trying to do in the easiest possible way.

Stephen Cleary
That was not what I was looking for. I think the problem is when I query using the Expression.Lambda<Func<T, object>>. I don't think the entity framework allow object to be cast as objects to database types.
Ganator
Take a step back and describe *what* you're trying to do, exactly.
Stephen Cleary