2014-12-04 23 views
5

Voglio creare Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> dall'elenco di SortingItems dinamicamente. Voglio dire che voglio creare la seguente espressione:Creazione di Func <IQueryable <TEntity>, IOrderedQueryable <TEntity>> dinamicamente?

entity => entity.OrderBy(c => c.Id).ThenBy(c => c.Name).ThenByDescending(c => c.LastName)

Quello che segue è il mio codice:

[DataContract] 
public class SortingItem 
{ 
    [DataMember] 
    public string PropertySelectorString { get; set; } 

    [DataMember] 
    public SortingDirectionsEnum SortingDirections { get; set; } 
} 

[DataContract] 
public enum SortingDirectionsEnum 
{ 
    [EnumMember] 
    Descending = 0, 

    [EnumMember] 
    Ascending = 1 
} 

public Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> GetSortingFunc<TEntity>() 
{ 
    _entityType = typeof(TEntity); 
    _parameterExpression = Expression.Parameter(_entityType, "entity"); 
    _iQueryableParameterExpression = Expression.Parameter(typeof(IQueryable<TEntity>), "f"); 

    MethodInfo orderByMethodInfo = null; 
    Expression resultExpression = null; 

    foreach (SortingItem sortingItem in SortingItems) 
    { 
     MemberExpression memberExpression = GetLeftSide(sortingItem.PropertySelectorString, _entityType, _parameterExpression); // I'm dead sure about working this line 

     switch (sortingItem.SortingDirections) 
     { 
      case SortingDirectionsEnum.Descending: 
       orderByMethodInfo = typeof(Queryable).GetMethods().First(method => method.Name == "OrderBy" && method.GetParameters().Length == 2).MakeGenericMethod(_entityType, memberExpression.Type); 
       if (resultExpression != null) 
        orderByMethodInfo = typeof(Queryable).GetMethods().First(method => method.Name == "ThenBy" && method.GetParameters().Length == 2).MakeGenericMethod(_entityType, memberExpression.Type); 
       break; 

      case SortingDirectionsEnum.Ascending: 
       orderByMethodInfo = typeof(Queryable).GetMethods().First(method => method.Name == "OrderByDescending" && method.GetParameters().Length == 2).MakeGenericMethod(_entityType, memberExpression.Type); 
       if (resultExpression != null) 
        orderByMethodInfo = typeof(Queryable).GetMethods().First(method => method.Name == "ThenByDescending" && method.GetParameters().Length == 2).MakeGenericMethod(_entityType, memberExpression.Type); 
       break; 
     } 

     MethodCallExpression methodCallExpression; 
     if (resultExpression != null) 
      // Exception 
      // An unhandled exception of type 'System.ArgumentException' occurred in System.Core.dll 
      // Additional information: Incorrect number of arguments supplied for call to method 'System.Linq.IOrderedQueryable`1[ConsoleApplication1.User] ThenBy[User,Int32](System.Linq.IOrderedQueryable`1[ConsoleApplication1.User], System.Linq.Expressions.Expression`1[System.Func`2[ConsoleApplication1.User,System.Int32]])' 
      methodCallExpression = Expression.Call(orderByMethodInfo, _iQueryableParameterExpression, resultExpression, Expression.Lambda(memberExpression, _parameterExpression)); 
     else 
      methodCallExpression = Expression.Call(orderByMethodInfo, _iQueryableParameterExpression, Expression.Lambda(memberExpression, _parameterExpression)); 

     resultExpression = Expression.Lambda(methodCallExpression, _iQueryableParameterExpression); 
    } 

    Expression<Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>> lambdaExpression = Expression.Lambda<Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>>(resultExpression, _parameterExpression); 
    return lambdaExpression.Compile(); 
} 

E 'OK per creare {f.OrderBy(entity => entity.Id)} o {f.ThenBy(entity => entity.Name)} separatamente, ma ottengo un'eccezione al seguente riga

methodCallExpression = Expression.Call(orderByMethodInfo, _iQueryableParameterExpression, resultExpression, Expression.Lambda(memberExpression, _parameterExpression)); 

Come utilizzare Expression.Call da combinare {f.OrderBy(entity => entity.Id)} con {f.ThenBy(entity => entity.Name)}, ....?

+0

si può spiegare ciò che è necessario che per? Forse c'è una soluzione molto più semplice al tuo "problema più grande" ... – ChrFin

+0

Ho creato 'SortingItems' nell'app client e lo invio al servizio. In Servizio voglio creare l'ordinamento 'func' da usare nel framework di entità –

risposta

2

Ti suggerisco di non scrivere tale codice, in quanto è difficile da leggere. Ho dovuto mantenere quei mostri in passato.

Se siete realmente interessati nella soluzione, è possibile scaricare DynamicLINQ, e quindi la query sarà come:

public string GetSortCriteria(this SortingItem item){ 
    return string.Format("{0} {1}", item.PropertySelectorString, 
      item.SortingDirections == SortingDirectionsEnum.Descending ? 
      "DESC" : "ASC"); 
} 

// later: 
var mergedSortCriteria= string.Join(",", 
    SortingItems.Select(item => item.GetSortCriteria()); 

Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> myFunc = 
     source => source.OrderBy("id " + mergedSortCriteria); 
Problemi correlati