这并不像看起来那么简单。你需要结合Expressions 或构建Expressions 来生成你想要的东西,不幸的是C#在这方面没有提供很多帮助。
最简单的方法是使用LambdaExpression 组合的扩展方法。这取决于一些 Expression 扩展方法,用于将 Expression 中的一个 Expression 替换为另一个 Expression:
public static class ExpressionExt {
// Compose: f.Compose(g) => x => f(g(x))
/// <summary>
/// Composes two LambdaExpression into a new LambdaExpression: f.Compose(g) => x => f(g(x))
/// </summary>
/// <param name="fFn">The outer LambdaExpression.</param>
/// <param name="gFn">The inner LambdaExpression.</param>
/// <returns>LambdaExpression representing outer composed with inner</returns>
public static Expression<Func<T, TResult>> Compose<T, TIntermediate, TResult>(this Expression<Func<TIntermediate, TResult>> fFn, Expression<Func<T, TIntermediate>> gFn) =>
Expression.Lambda<Func<T, TResult>>(fFn.Body.Replace(fFn.Parameters[0], gFn.Body), gFn.Parameters[0]);
/// <summary>
/// Replaces a sub-Expression with another Expression inside an Expression
/// </summary>
/// <param name="orig">The original Expression.</param>
/// <param name="from">The from Expression.</param>
/// <param name="to">The to Expression.</param>
/// <returns>Expression with all occurrences of from replaced with to</returns>
public static Expression Replace(this Expression orig, Expression from, Expression to) => new ReplaceVisitor(from, to).Visit(orig);
}
/// <summary>
/// Standard ExpressionVisitor to replace an Expression with another in an Expression.
/// </summary>
public class ReplaceVisitor : ExpressionVisitor {
readonly Expression from;
readonly Expression to;
public ReplaceVisitor(Expression from, Expression to) {
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node) => node == from ? to : base.Visit(node);
}
现在您可以创建您的方法,该方法采用代表您要测试的字段的 lambda。它使用本地的LambdaExpression 作为最终结果的模板:
public static class Util {
static Expression<Func<string, int>> TermOrderTemplateFn = p => (p == "Fall" ? 1 : p == "Spring" ? 2 : 3);
public static Expression<Func<TRec, int>> TermsOrder<TRec>(Expression<Func<TRec, string>> selectorFn) =>
TermOrderTemplateFn.Compose(selectorFn);
}
现在您可以在表达式中调用该方法,传入一个表示所需字段(或字段表达式)的 lambda 进行测试:
var tmp = context.Terms.Include(x => x.StudentCourses).AsQueryable()
.Where(x => x.StudentID == studentId && x.DepartmentID == departmentId)
.OrderBy(x => x.AcademicYear)
.ThenBy(Util.TermsOrder<Term>(p => p.TermRegistered));
注意:我正在调用 context.Terms.First() Term 的类型,但您需要在调用 TermsOrder 时使用实际正确的类型名称。你也可以改用TermsOrder((Term p) => ...)。
我可能更愿意创建一个特殊版本的ThenBy,这样您就可以使用类型推断来确定记录类型:
public static class EFExt {
static Expression<Func<string, int>> TermThenOrderTemplateFn = p => (p == "Fall" ? 1 : p == "Spring" ? 2 : 3);
public static IOrderedQueryable<T> ThenByTerm<T>(this IOrderedQueryable<T> src, Expression<Func<T, string>> selectorFn) =>
src.ThenBy(TermThenOrderTemplateFn.Compose(selectorFn));
}
那么就可以直接使用了:
var tmp = context.Terms.Include(x => x.StudentCourses).AsQueryable()
.Where(x => x.StudentID == studentId && x.DepartmentID == departmentId)
.OrderBy(x => x.AcademicYear)
.ThenByTerm(p => p.TermRegistered);