【问题标题】:Dynamic LINQ OrderBy + Method Count动态 LINQ OrderBy + 方法计数
【发布时间】:2012-10-17 19:56:15
【问题描述】:

我在网页中显示一个对象表Company,我正在使用动态 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]])'

你知道如何实现吗?

【问题讨论】:

  • 你遇到了什么异常?
  • exprtype的值是多少?
  • 其余代码在此处stackoverflow.com/questions/41244/dynamic-linq-orderby/… expr 是 Expression 类型,在我的示例中类型为“Employee”。为什么投反对票?
  • 好吧,我刚刚在 Linq To Entities 和 Linq To Object 中进行了测试,Marc Gravell 的代码在这两种情况下都可以正常运行,无需更改。您确定您的公司课程中有public virtual IList&lt;Employee&gt; Employees 吗?
  • 你确定是拉斐尔吗?我正在使用 Linq to Entities,但我也使用 Linq To Object 和像“Employees.Count”这样的字符串进行了测试,它不起作用。我用这段代码测试过:gist.github.com/3899759

标签: c# linq


【解决方案1】:

从您下面的gist 中,我找到了一种简单的方法来展平所有基本类型和接口的属性,如post 所示。

所以我实现了 PropertyInfo 的扩展方法,它将返回该类型继承的所有接口和基类的所有属性。问题是 IList 没有 Count 属性,但 iCollection 有。 public static PropertyInfo[] GetPublicProperties(this Type type) 将平展所有属性,我们从那里得到正确的属性,这应该适用于现在的任何属性,而不仅仅是 Count。

public class Program
{
    private static IList<Company> _companies;


    static void Main(string[] args)
    {
        var sort = "Employees.Count";
        _companies = new List<Company>();


        _companies.Add(new Company
                           {
                               Name = "c2",
                               Address = new Address {PostalCode = "456"},
                               Employees = new List<Employee> {new Employee(), new Employee()}
                           });
        _companies.Add(new Company
                           {
                               Name = "c1",
                               Address = new Address {PostalCode = "123"},
                               Employees = new List<Employee> { new Employee(), new Employee(), new Employee() }
                           });

        //display companies
        _companies.AsQueryable().OrderBy(sort).ToList().ForEach(c => Console.WriteLine(c.Name));


        Console.ReadLine();
    }
}


public class Company
{
    public string Name { get; set; }
    public Address Address { get; set; }
    public IList<Employee> Employees { get; set; }
}

public class Employee{}


public class Address
{
    public string PostalCode { get; set; }
}


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


    public 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.GetPublicProperties().FirstOrDefault(c => c.Name == prop);
            if (pi != null)
            {
                expr = Expression.Property(expr, pi);
                type = pi.PropertyType;
            }
            else { throw new ArgumentNullException(); }
        }
        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 static PropertyInfo[] GetPublicProperties(this Type type)
    {
        if (type.IsInterface)
        {
            var propertyInfos = new List<PropertyInfo>();

            var considered = new List<Type>();
            var queue = new Queue<Type>();
            considered.Add(type);
            queue.Enqueue(type);
            while (queue.Count > 0)
            {
                var subType = queue.Dequeue();
                foreach (var subInterface in subType.GetInterfaces())
                {
                    if (considered.Contains(subInterface)) continue;

                    considered.Add(subInterface);
                    queue.Enqueue(subInterface);
                }

                var typeProperties = subType.GetProperties(
                    BindingFlags.FlattenHierarchy
                    | BindingFlags.Public
                    | BindingFlags.Instance);

                var newPropertyInfos = typeProperties
                    .Where(x => !propertyInfos.Contains(x));

                propertyInfos.InsertRange(0, newPropertyInfos);
            }

            return propertyInfos.ToArray();
        }

        return type.GetProperties(BindingFlags.FlattenHierarchy
            | BindingFlags.Public | BindingFlags.Instance);
    }
}

【讨论】:

  • 那么,如果我输入“Employees.Count”,你将如何使这段代码工作? gist.github.com/3899759
  • 您只需要查看您的类型值并查看它是否是泛型类型,如果是,您需要在 MakegenericMethod 调用中使用它的泛型类型参数。但这仅适用于硬编码的“计数”方法。
  • 这就是我所做的:gist.github.com/3904846。您将如何更正此代码gist.github.com/3899759
  • 上面的代码可以处理任意数量的属性和嵌套属性,只要它们不为空。
猜你喜欢
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多