0

我使用这段代码(在stackoverflow上找到)生成一个“OrderBy”

static class BuilderOrder
{
    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "OrderBy");
    }

    public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "OrderByDescending");
    }

    public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "ThenBy");
    }

    public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
    {
        return ApplyOrder<T>(source, property, "ThenByDescending");
    }

    static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
    {
        string[] props = property.Split('.');
        Type type = typeof(T);
        ParameterExpression arg = Expression.Parameter(type, "x");
        Expression expr = arg;

        foreach (string prop in props)
        {
            // use reflection (not ComponentModel) to mirror LINQ
            PropertyInfo pi = type.GetProperty(prop);
            expr = Expression.Property(expr, pi);
            type = pi.PropertyType;
        }

        Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
        LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

        object result = typeof(Queryable).GetMethods().Single(
                method => method.Name == methodName
                        && method.IsGenericMethodDefinition
                        && method.GetGenericArguments().Length == 2
                        && method.GetParameters().Length == 2)
                .MakeGenericMethod(typeof(T), type)
                .Invoke(null, new object[] { source, lambda });
        return (IOrderedQueryable<T>)result;
    }
}

我有这个对象:

public class Person : IPerson
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public interface IPerson
{
    int Id { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
}

是这样使用的

private void Tri(IQueryable<IPerson> list, string field, string direction)
{
    if (direction.ToUpper() == "DESC")
    {
        return BuilderOrder.OrderByDescending(liste, field);
    }
    else
    {
        return BuilderOrder.OrderBy(liste, field);
    }
}

问题,排序不起作用。我想要一种作为参数发送的方法,要排序的列表IQueryable<IPerson>,更好IQueryable<T>的,字段名(用于排序)和方向(asc/desc)

任何的想法 ?

谢谢,

4

1 回答 1

0

我可以相信它不起作用......因为你在一个 void 方法中工作,没有 ref 参数!

将您的方法 Tri (将其重命名为 SortBy 或一些英文名称会更好)在 BuilderOrder 类中,然后

public static IOrderedQueryable<T> SortBy(this IQueryable<T> queryable, string field, string direction)
{
    return (direction.ToUpper() == "DESC") 
       ? queryable.OrderByDescending(field)
       : queryable.OrderBy(field);
}

你可以添加另一个:

public static IOrderedQueryable<T> SortThenBy(this IOrderedQueryable<T> orderedQueryable, string field, string direction)
{
    return (direction.ToUpper() == "DESC") 
       ? orderedQueryable.ThenByDescending(field)
       : orderedQueryable.ThenBy(field);
}
于 2012-06-29T07:56:59.400 回答