How do you remove OrderBy expression from an ExpressionTree using a ExpressionVisitor?

555 Views Asked by At

The Orderby statment is not supported by the Azure Table storage linq provider I have an Expression like

.Where(t => (t.RowKey.CompareTo("U_") > 0)).OrderBy(user => user.UserName)

i'm trying to remove .OrderBy(user => user.UserName) from the expression tree

I would also like the visitor to remove the orderby statment from the following expression

.Where(t => (t.RowKey.CompareTo("U_") > 0)).OrderBy(user => user.UserName).Take(10)

will become

.Where(t => (t.RowKey.CompareTo("U_") > 0)).Take(10)
1

There are 1 best solutions below

0
On BEST ANSWER

Here's a visitor implementation.

class OrderByRemovalVisitor : ExpressionVisitor
{

    protected override Expression VisitMethodCall(MethodCallExpression node)
    {

        if (node.Method.Name == "OrderBy" && node.Method.DeclaringType == typeof(Queryable))
            return node.Arguments[0];
        else
            return base.VisitMethodCall(node);
    }
}