2

经过大量的谷歌搜索并尝试了一些事情并没有找到/得到想要的结果后,我决定发布这个问题。

我有一个定制的OrderBy扩展方法,现在在执行OrderBy操作时我想传递一个AlphanumComparator这样的:

return divergences.OrderBy(sort, new AlphanumComparator());

这是扩展方法:

public static IQueryable<T> OrderBy<T>(this IQueryable<T> collection,
    GridSortOptions sortOptions, AlphanumComparator comparer = null)
{
    if (string.IsNullOrEmpty(sortOptions.Column))
    {
        return collection;
    }

    Type collectionType = typeof(T);

    ParameterExpression parameterExpression = Expression.Parameter(collectionType, "p");

    Expression seedExpression = parameterExpression;

    Expression aggregateExpression = sortOptions.Column.Split('.').Aggregate(seedExpression, Expression.Property);

    MemberExpression memberExpression = aggregateExpression as MemberExpression;

    if (memberExpression == null)
    {
        throw new NullReferenceException(string.Format("Unable to cast Member Expression for given path: {0}.", sortOptions.Column));
    }

    LambdaExpression orderByExp = Expression.Lambda(memberExpression, parameterExpression);

    const string orderBy = "OrderBy";

    const string orderByDesc = "OrderByDescending";

    Type childPropertyType = ((PropertyInfo)(memberExpression.Member)).PropertyType;

    string methodToInvoke = sortOptions.Direction == MvcContrib.Sorting.SortDirection.Ascending ? orderBy : orderByDesc;

    MethodCallExpression orderByCall;

    orderByCall = Expression.Call(typeof(Queryable), methodToInvoke, new[] { collectionType, childPropertyType }, collection.Expression, Expression.Quote(orderByExp));

    if(comparer != null)
    {
       // How can I pass the comparator to the OrderBy MethodCallExpression?

       // Using the standard LINQ OrderBy, we can do this:
       // elements.OrderBy(e => e.Index, new AlphanumComparator())
    }

    return collection.Provider.CreateQuery<T>(orderByCall);
}

请参阅我认为应该通过的代码中的注释IComparer...我该如何处理?

4

1 回答 1

0

我不得不以不同的方式处理这个问题。

我试图创建一个OrderByMvcContrib Grid一起使用的泛型,但是将 传递IComparer给该自定义OrderBy表达式并没有像我想象的那样工作。

所以我创建了这个助手,它接收一个点表示法的字符串,Element1.Standard.Chapter.Manual.Name然后返回一个Expression<Func<T, string>>

public static Func<T, string> CreateSelectorExpression<T>(string propertyName) where T : class
{
    ParameterExpression parameterExpression = Expression.Parameter(typeof(T));

    Expression aggregateExpression = propertyName.Split('.').
        Aggregate(parameterExpression as Expression, Expression.Property) as MemberExpression;

    LambdaExpression exp = Expression.Lambda(aggregateExpression, parameterExpression);

    return (Func<T, string>)exp.Compile();
}

Divergence然后可以将这个类型为 T(在本例中为对象类型)的表达式传递(参见func.Invoke参考资料)给标准 LINQOrderBy运算符,在这里我也可以IComparer AlphanumComparator像这样传递自定义:

if (sort.Column.Contains("Index"))
{
    var func = Helpers.ExtensionMethods.CreateSelectorExpression<Divergence>(sort.Column);

    if (sort.Direction == SortDirection.Ascending)
    {
        return divergences.OrderBy(func, new AlphanumComparator());
    }
    else
    {
        return divergences.OrderByDescending(func, new AlphanumComparator());
    }
}

这涉及更多的工作,但以我想要的方式以通用方式解决了问题。

于 2013-08-01T02:35:40.627 回答