2012-08-31 2 views

답변

9

코드 첫째, 설명 나중에.

IQueryable<T> data = this.Database.ObjectsOfType<T>(); 

var eachItem = Expression.Parameter(typeof(T), "item"); 
var propertyToOrderByExpression = Expression.Property(eachItem, propertyName); 

var runMe = Expression.Call(
    typeof(Queryable), 
    "OrderBy", 
    new Type[] { data.ElementType, typeof(IComparable) }, 
    data.Expression, 
    Expression.Lambda<Func<T,IComparable>>(propertyToOrderByExpression, new ParameterExpression[] { eachItem })); 

그래서 먼저 데이터를 Queryable 객체로 유지합니다. 이것은 일종의 'root'Expression 속성을 가지고 있으며 우리는 그것을 필요로합니다.

eachItem 객체는 람다의 인수 자리 표시자를 나타내는 표현식이며, 표시되는 심볼은 원하는 경우 표시됩니다.

그러면 우리는 propertyName에서 사용자가 지정한 속성 이름을 읽기 작업을 수행하는 식으로 만듭니다.

마지막으로 Queryable 데이터에서 OrderBy 메서드를 호출하는 식을 작성합니다. 우리는 (인수의 순서로) 말을하는지 :

Expression.Call(
[what's the type on which we want to call a method?], 
[what's the name of the method we're calling?], 
[if this method is generic, what are the types it deals with?], 
{ 
    [expression representing the data], 
    [expression for the lambda using the reader exp + each item exp] 
}) 

마지막 두가 실제로 PARAM 배열 때문에} {에 있습니다. 속성은 어떤 유형이 될 수 있기 때문에 IComparable을 사용했으나 분명히 정렬 할 수 있어야합니다.

루크

관련 문제