使用下面的代码,我没有任何问题可以排序FirstName
,但我也LastName
希望能够排序。是否有对属性进行排序的解决方案,并且该属性是“复杂对象”而不是原始对象?Name
Code
谢谢,
我的对象:
public class Person
{
public Language Language { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Language
{
public string Name { get; set; }
public string Code { get; set; }
}
我有这段代码要排序:
var type = typeof(T);
var property = type.GetProperty("OrderBy");
var parameter = Expression.Parameter(type, "p");
var propertyAccess = Expression.MakeMemberAccess(parameter, property);
var orderByExp = Expression.Lambda(propertyAccess, parameter);
MethodCallExpression resultExp =
Expression.Call(typeof(Queryable),
"OrderBy",
new Type[] { type, property.PropertyType },
source.Expression,
Expression.Quote(orderByExp));
return source.Provider.CreateQuery<T>(resultExp);