我无法对此进行测试,但似乎它应该可以工作。最后没有平方根,但顺序应该是相同的。
public static IOrderedQueryable<T> EuclideanDistanceOrder<T>(this IQueryable<T> query, IEnumerable<Expression<Func<T, double>>> expressions)
{
var parameter = Expression.Parameter(typeof(T), "item");
var seed = Expression.Lambda<Func<T, double>>(Expression.Constant((double)0), parameter);
return query.OrderBy(expressions.Aggregate(seed, GetAggregateExpression));
}
private static Expression<Func<T, double>> GetAggregateExpression<T>(Expression<Func<T, double>> sum, Expression<Func<T, double>> item)
{
var parameter = Expression.Parameter(typeof(T), "item");
return Expression.Lambda<Func<T, double>>(Expression.Add(Expression.Invoke(sum, parameter), Expression.Power(Expression.Invoke(item, parameter), Expression.Constant((double)2))), parameter);
}
编辑:
由于您不能使用Expression.Invoke()
,因此您需要内联传递给EuclideanDistanceOrder
. 似乎没有任何“好”的方法可以做到这一点,所以我写了一个Replace
方法来做到这一点。我只Replace
为一些更常见的Expression
类型实现了,希望这足以涵盖您的使用,但您可能需要为其他Expression
类型实现它。
public static IOrderedQueryable<T> EuclideanDistanceOrder<T>(this IQueryable<T> query, IEnumerable<Expression<Func<T, double>>> expressions)
{
var parameter = Expression.Parameter(typeof(T), "item");
var seed = Expression.Constant((double)0);
var agg = expressions.Aggregate((Expression)seed, (s, item) => Expression.Add(s, Expression.Power(Replace(item.Body, item.Parameters[0], parameter), Expression.Constant((double)2))));
return query.OrderBy(Expression.Lambda<Func<T, double>>(agg, parameter));
}
private static Expression Replace(Expression expression, ParameterExpression original, ParameterExpression replacement)
{
if (expression is BinaryExpression)
{
var binaryExpression = (BinaryExpression)expression;
return Expression.MakeBinary(expression.NodeType, Replace(binaryExpression.Left, original, replacement), Replace(binaryExpression.Right, original, replacement), binaryExpression.IsLiftedToNull, binaryExpression.Method, binaryExpression.Conversion);
}
if (expression is ConditionalExpression)
{
var conditionalExpression = (ConditionalExpression)expression;
return Expression.Condition(Replace(conditionalExpression.Test, original, replacement), Replace(conditionalExpression.IfTrue, original, replacement), Replace(conditionalExpression.IfFalse, original, replacement), conditionalExpression.Type);
}
if (expression is ConstantExpression)
{
return expression;
}
if (expression is MemberExpression)
{
var memberExpression = (MemberExpression)expression;
return Expression.MakeMemberAccess(Replace(memberExpression.Expression, original, replacement), memberExpression.Member);
}
if (expression is ParameterExpression)
{
var parameterExpression = (ParameterExpression)expression;
return parameterExpression == original ? replacement : parameterExpression;
}
if (expression is UnaryExpression)
{
var unaryExpression = (UnaryExpression)expression;
return Expression.MakeUnary(unaryExpression.NodeType, Replace(unaryExpression.Operand, original, replacement), unaryExpression.Type, unaryExpression.Method);
}
throw new Exception(string.Format("Unsupported expression type: {0}", expression.NodeType));
}
例如,如果我们的输入表达式是:
p => p.X1 - p.X2
p => p.Y1 - p.Y2
最初的实现将构建:
i => 0 + expressions[0](i) ^ 2 + expressions[1](i) ^ 2
新实现采用原始表达式,并将输入参数(p
在上面)替换为将传递给最终 lambda ( i
) 的参数,并在输出中直接使用表达式的主体:
i => 0 + (i.X1 - i.X2) ^ 2 + (i.Y1 - i.Y2) ^ 2