ソート用の式を作成しようとしていますが、1 つのプロパティを使用してリストをソートするコードを書きました。
しかし、最初にあるプロパティで並べ替え、次に別のプロパティで並べ替える必要があります。
つまり、次のようなものを実装する式を作成したいということです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());