I haven't been able to test this, but it seems that it ought to work. There's no square-root at the end, but the order should be the same either way.
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);
}
Edit:
Since you can't use Expression.Invoke()
, you'll need to inline the bodies of the Expressions passed into EuclideanDistanceOrder
. There doesn't seem to be any "nice" way to do this, so I've written a Replace
method to do it. I've only implemented Replace
for some of the more common Expression
types, hopefully this will be enough to cover your usage, but you may need to implement it for other Expression
types.
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));
}
So if for example, our input expressions are:
p => p.X1 - p.X2
p => p.Y1 - p.Y2
The original implementation would've constructed:
i => 0 + expressions[0](i) ^ 2 + expressions[1](i) ^ 2
The new implementation takes the original expression, and replaces the input parameter (p
in the above) with the parameter that will be passed to the final lambda (i
), and uses the body of the expression directly in the output:
i => 0 + (i.X1 - i.X2) ^ 2 + (i.Y1 - i.Y2) ^ 2
Math.Pow(i,2)
can be replaced withi*i
; – Bob Vale Jul 20 '12 at 7:57