1

我有一个表达式访问者将表达式转换为 url 格式。但它只转换最后调用的表达式。例如,如果我这样称呼我的收藏:

NetworkAccountStorage.Where<NetworkAccountModel>(x => x.ID + 1 > 0).Select(x => x.Name).Distinct()

Distinct 将是唯一访问的表达式。如何解决这个问题?

protected override Expression VisitMethodCall(MethodCallExpression m)
    {
        if (m.Method.DeclaringType == typeof(Queryable) && m.Method.Name == "Where")
        {
            sb.Append("$filter=");
            //this.Visit(m.Arguments[0]);
            //sb.Append(") AS T WHERE ");
            LambdaExpression lambda = (LambdaExpression)StripQuotes(m.Arguments[1]);
            this.Visit(lambda.Body);
            return m;
        }
        else if (m.Method.DeclaringType == typeof(Queryable) && m.Method.Name == "Select")
        {
            sb.Append("$select=");
            LambdaExpression lambda = (LambdaExpression)StripQuotes(m.Arguments[1]);
            this.Visit(lambda.Body);
            return m;
        }

        throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name));
    }
4

2 回答 2

3

您需要通过调用您覆盖的基本方法进行递归:

base.VisitMethodCall(...);
于 2012-12-28T22:27:33.560 回答
1

你需要递归。Distinct 接受一个this参数,即 .Select 调用等。

于 2012-12-28T22:07:35.093 回答