动态LINQ OrderBy +方法计数

时间:2012-10-08 09:10:12

标签: c# linq

我在网页中显示了一个对象公司的表格,我正在使用动态Linq OrderBy对每个属性进行排序。 我正在使用此代码https://stackoverflow.com/a/233505/265122

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

这很棒,但我也想对公司的员工数量进行分类。

像这样: query.OrderBy(“Employees.Count”)

到目前为止,我试图动态调用Count方法,但没有任何成功。

我修改了这样的代码:

foreach(string prop in props)
{
    if (prop == "Count")
    {
        var countMethod = (typeof(Enumerable)).GetMethods().First(m => m.Name == "Count").MakeGenericMethod(type);
        expr = Expression.Call(countMethod, expr);
        break;
    }

    // Use reflection (not ComponentModel) to mirror LINQ.
    PropertyInfo pi = type.GetProperty(prop);
    expr = Expression.Property(expr, pi);
    type = pi.PropertyType;
}

但我在expr = Expression.Call(countMethod, expr);

上有例外

例外是:

ArgumentException
Expression of type 'System.Collections.Generic.ICollection`1[Employee]'
cannot be used for parameter of type
'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.ICollection`1
[Employee]]' of method 'Int32 Count[ICollection`1]
System.Collections.Generic.IEnumerable`1[System.Collections.Generic.ICollection`1
Employee]])'

关于如何实现这一点的任何想法?

0 个答案:

没有答案