创建Func <iqueryable <tentity>,IOrderedQueryable <tentity>&gt;动态?</tentity> </IQueryable的<tentity>

时间:2014-12-04 12:03:39

标签: c# entity-framework sorting lambda expression

我想动态地从Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>列表中创建SortingItems。 我的意思是我想创建以下表达式:

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

以下是我的代码:

[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();
}

单独创建{f.OrderBy(entity => entity.Id)}{f.ThenBy(entity => entity.Name)}是可以的,但我在以下行获得例外

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

我应该如何使用Expression.Call{f.OrderBy(entity => entity.Id)}{f.ThenBy(entity => entity.Name)}合并,....?

1 个答案:

答案 0 :(得分:2)

我建议你不要写这样的代码,因为它很难阅读。我过去不得不维护那些怪物。

如果您真的对该解决方案感兴趣,可以下载DynamicLINQ,然后您的查询将如下:

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);