Builder orderinf表达式,字段名称和方向作为参数

时间:2012-06-29 07:23:23

标签: c# linq linq-to-sql predicate

我使用这段代码(在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)

有什么想法吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

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

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

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