Reputation: 967
I have
class User
{
public string Name {get; set; }
}
and
class Example
{
public virtual User user {get; set; }
}
they are 1..N relation and I'm using EF and that all works fine, but I want to do
var model = examples.OrderBy("user.Name");
where examples is IQueryable of Examples and I get error that Example has no property of user.Name. Why is that? Can I specify my compare rules for class User? I tried implementing ICompare and IComparable with no success.
EDIT: this is the extension for orderBy, how can I modify it to be able to use it for my example
public static IQueryable OrderBy(this IQueryable source, string propertyName)
{
var x = Expression.Parameter(source.ElementType, "x");
var selector = Expression.Lambda(Expression.PropertyOrField(x, propertyName), x);
return source.Provider.CreateQuery(
Expression.Call(typeof(Queryable), "OrderBy", new Type[] { source.ElementType, selector.Body.Type },
source.Expression, selector
));
}
}
Upvotes: 0
Views: 267
Reputation: 161
I think you're looking for:
List<User> SortedList = examples.OrderBy(u=>u.Name).ToList();
You must be using Linq
Upvotes: 0
Reputation: 77576
The straightforward:
examples.OrderBy(x => x.user.Name);
Should work just fine. However, normally OrderBy
does not consume a string -- is there a reason you were leveraging that syntax?
Upvotes: 3