5

I'm displaying a table of objects Company in a webpage and I am using a Dynamic Linq OrderBy to sort them on each property. I'm using this code 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;
}

It's great but I would also like to sort the companies on the number of employees.

Like this : query.OrderBy("Employees.Count")

I tried to call to the Count method dynamically without any success so far.

I modified the code like this :

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

But I have an exception on the expr = Expression.Call(countMethod, expr);

The exception is:

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]])'

Any idea on how to achieve that?

Community
  • 1
  • 1
Julien
  • 833
  • 8
  • 20
  • what's the exception you're getting? – eyal Oct 08 '12 at 09:32
  • What are the values of `expr` and `type`? – Thom Smith Oct 08 '12 at 15:00
  • The rest of the code is here http://stackoverflow.com/questions/41244/dynamic-linq-orderby/233505#233505 expr is of type Expression and in my example type would by "Employee". Why the downvote? – Julien Oct 08 '12 at 15:21
  • Well, I just tested in Linq To Entities and in Linq To Object, and Marc Gravell's code is working perfectly without change in both cases. Are you sure you've got a `public virtual IList Employees` in your Company class ? – Raphaël Althaus Oct 10 '12 at 21:02
  • Are you sure Raphaël? I'm using Linq to Entities but I also tested with Linq To Object and with a string like "Employees.Count" it is not working. I tested with this code: https://gist.github.com/3899759 – Julien Oct 16 '12 at 15:10

1 Answers1

3

From your gist below I have found a easy way t flatten the properties across all base types and interfaces as demonstrated on this post.

So I implemented the extension method for PropertyInfo that will return all properties from all interfaces and base classes inherited by the type. The problem was that IList does not have a Count property however iCollection does. The public static PropertyInfo[] GetPublicProperties(this Type type) will flat all properties and we get the correct one from there this should work on any property now not only 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);
    }
}
Community
  • 1
  • 1
dmportella
  • 4,614
  • 1
  • 27
  • 44
  • So How would you make this code work if I enter "Employees.Count"? https://gist.github.com/3899759 – Julien Oct 16 '12 at 15:13
  • you just need to look into you type value and see if it is a generic type if so you need to use the generic type parameter of it on the MakegenericMethod call. But that will work only of the hard coded "Count" method. – dmportella Oct 16 '12 at 20:01
  • 1
    That's what I did with this : https://gist.github.com/3904846 . How would you correct this code https://gist.github.com/3899759 ? – Julien Oct 17 '12 at 10:28
  • the code above will work with any number of properties and nested properties as long as they are not null. – dmportella Oct 17 '12 at 11:59