build an expression with multiple sorting
Asked Answered
L

2

7

I am trying to build an expression for sorting, and i wrote code that sorts my list using one property.

But I need to sort it firstly by one property, secondly by another property and so on.

I mean I want to build an expression that will implement something like that: students.OrderBy(fistExpression.Compile()).ThenBy(secondImpression.Complie()).ThenBy(thirdExpression.Compile()).

So how to dynamically put that ThenBy methods?

Here is my code:

Type studentType = typeof(Student);
ParameterExpression studentParam = Expression.Parameter(studentType, "x");
MemberInfo ageProperty = studentType.GetProperty("Age");
MemberExpression valueInNameProperty = 
     Expression.MakeMemberAccess(studentParam, ageProperty);
Expression<Func<Student, int>> orderByExpression =
     Expression<Func<Student, int>>.Lambda<Func<Student, int>>(valueInNameProperty, studentParam);
var sortedStudents = students.OrderBy(orderByExpression.Compile());
Litigable answered 12/1, 2013 at 13:28 Comment(2)
To clarify - you're looking for a way to build a single Expression you can pass to .OrderBy that will replicate the functionality of passing a series of Expressions to .OrderBy and .ThenBy, correct?Anitaanitra
Dupe https://mcmap.net/q/11365/-dynamic-linq-orderby-on-ienumerable-lt-t-gt-iqueryable-lt-t-gtJosettejosey
D
2

My solution:

public static Func<Student, object> BuildPredicate(string propertyName)
{
    Type studentType = typeof(Student);
    ParameterExpression studentParam = Expression.Parameter(studentType, "x");
    MemberInfo ageProperty = studentType.GetProperty(propertyName);
    MemberExpression valueInNameProperty = Expression.MakeMemberAccess(studentParam, ageProperty);
    UnaryExpression expression = Expression.Convert(valueInNameProperty, typeof (object));
    Expression<Func<Student, object>> orderByExpression = Expression.Lambda<Func<Student, object>>(expression, studentParam);
    return orderByExpression.Compile();
}

in your expression making code is added casting to object.

That is how you can create a chain of ThenBy:

var sortedStudents = students.OrderBy(BuildPredicate("Age"));
foreach (var property in typeof(Student).GetProperties().Where(x => !String.Equals(x.Name, "Age")))
{
    sortedStudents = sortedStudents.ThenBy(BuildPredicate(property.Name));
}

var result = sortedStudents.ToList();

Finally, Student sample class:

public class Student
{
    public int Age { get; set; }
    public string Name { get; set; } 
}

Update:

Another approach is using attributes to mark properies from your Student to use them in OrderBy and ThenBy. Like:

public class Student
{
    [UseInOrderBy]
    public int Age { get; set; }

    [UseInOrderBy(Order = 1)]
    public string Name { get; set; } 
}

[AttributeUsage(AttributeTargets.Property)]
internal class UseInOrderByAttribute : Attribute
{
    public int Order { get; set; }
}

That is how you can build sorting chain using UseInOrderByAttribute:

Type studentType = typeof (Student);
var properties = studentType.GetProperties()
                            .Select(x => new { Property = x, OrderAttribute = x.GetCustomAttribute<UseInOrderByAttribute>() })
                            .Where(x => x.OrderAttribute != null)
                            .OrderBy(x => x.OrderAttribute.Order);

var orderByProperty = properties.FirstOrDefault(x => x.OrderAttribute.Order == 0);
if (orderByProperty  == null)
    throw new Exception("");

var sortedStudents = students.OrderBy(BuildPredicate(orderByProperty.Property.Name));
foreach (var property in properties.Where(x => x.Property.Name != orderByProperty.Property.Name))
{
    sortedStudents = sortedStudents.ThenBy(BuildPredicate(property.Property.Name));
}

var result = sortedStudents.ToList();

Fix: BuildPredicate can be writen without dynamic. BuildPredicate sample code is changed.

Delftware answered 12/1, 2013 at 13:53 Comment(0)
C
1

I assume that you have private properties that you want to be able to sort. If you for example have this class:

public class Student 
{
    public Student (int age, string name)
    {
        Age = age;
        Name = name;
    }

    private string Name { get;  set; }

    public int Age { get; set; }

    public override string ToString ()
    {
        return string.Format ("[Student: Age={0}, Name={1}]", Age, Name);
    }
}

You can use the following method to build expressions that will get both public and private properties:

public static Func<TType, TResult> CreateExpression<TType, TResult>(string propertyName) 
{
    Type type = typeof(TType);
    ParameterExpression parameterExpression = Expression.Parameter(type, propertyName);
    MemberInfo property = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    MemberExpression valueInProperty =  Expression.MakeMemberAccess(parameterExpression, property);

    return Expression.Lambda<Func<TType,TResult>>(valueInProperty, parameterExpression).Compile();
}

Example of usage:

        var students = new [] { 
            new Student(20, "Ben"), 
            new Student(20, "Ceasar"),
            new Student(20, "Adam"),
            new Student(21, "Adam"),
        };

        var sortedStudents = students
            .OrderBy(CreateExpression<Student, string>("Name"))
            .ThenBy(CreateExpression<Student, int>("Age"));

        sortedStudents.ToList().ForEach(student => Console.WriteLine(student)); 
        /*
        Prints:
        [Student: Age=20, Name=Adam]
        [Student: Age=21, Name=Adam]
        [Student: Age=20, Name=Ben]
        [Student: Age=20, Name=Ceasar]
         */
Confederacy answered 12/1, 2013 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.