c# - 构建具有多重排序的表达式

标签 c# linq lambda expression

我正在尝试构建一个用于排序的表达式,并且我编写了使用一个属性对我的列表进行排序的代码。

但我需要先按一个属性对它进行排序,然后再按另一个属性排序,依此类推。

我的意思是我想构建一个表达式来实现类似的东西:students.OrderBy(fistExpression.Compile()).ThenBy(secondImpression.Complie()).ThenBy(thirdExpression.Compile()).

那么如何动态地放置ThenBy方法呢?

这是我的代码:

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

最佳答案

我的解决方案:

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

在您的表达式中,将代码添加到 object 中。

这就是创建 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();

最后,Student示例类:

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

更新:

另一种方法是使用属性来标记 Student 中的特性,以便在 OrderByThenBy 中使用它们。喜欢:

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

这就是使用 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();

修复:BuildPredicate 可以在没有dynamic 的情况下编写。 BuildPredicate 示例代码已更改。

关于c# - 构建具有多重排序的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14293760/

相关文章:

c# - 将 DateTime.Add(TimeSpan) 与 LINQ 结合使用

linq - NHibernate.Linq 和 CompareTo 字符串

c# - 具有 lambda 表达式的事件

c# - 将数据表复制为另一个数据表中的列

c# - 如何构造一个 try-catch-finally block 来处理 finally 内部的错误?

c# - Linq - boolean - where 语句

存储在容器中的 C++ Lambda 捕获 "this"

java - 查找和修改 ArrayList 中的特定元素

c# - 从代码隐藏调用 javascript 函数的框架

c# - 确定最低兼容 API 版本