English 中文(简体)
言论自由
原标题:ExpressionHelper.GetExpressionText(expression) not returning the name of my property
最佳回答

这是众所周知的行为。 我用自己的版本(ExpressionHelper处理该具体案件。 现在有两种选择:

  1. 利用NuGet一揽子计划:

    Install-Package Mariuzzo.Web.Mvc.Extras
    
  2. Or Just grab The source Code of the above expressionHelper, and glue it into You project.

问题回答

利用这一职能:

    static public string GetExpressionText(LambdaExpression p)
    {
        if (p.Body.NodeType == ExpressionType.Convert || p.Body.NodeType == ExpressionType.ConvertChecked)
        {
            p = Expression.Lambda(((UnaryExpression)p.Body).Operand,
                p.Parameters);
        }
        return ExpressionHelper.GetExpressionText(p);
    }

此处为:

    public static void AddModelError<TModel>(this ModelStateDictionary state, Expression<Func<TModel, object>> expression, string message)
    {
        LambdaExpression lambdaExpression = null;
        string fieldName = string.Empty;

        if (expression.Body.NodeType == ExpressionType.Convert || expression.Body.NodeType == ExpressionType.ConvertChecked)
        {
            lambdaExpression = Expression.Lambda(((UnaryExpression)expression.Body).Operand, expression.Parameters);
            fieldName = ExpressionHelper.GetExpressionText(lambdaExpression);
        } else {
            fieldName = ExpressionHelper.GetExpressionText(expression);
        }

        state.AddModelError(fieldName, message);
    }

这是一种更紧凑的办法,可能是一个更好的解决办法:

https://stackoverflow.com/a/12689563/951001

如果你不使用不可否认的类型,就会放弃,那么看来是行之有效的(即删除?) 过了两倍,或像我的案件一样。 So

Expression<Func<TModel, double?>> 

......

Expression<Func<TModel, double>>.  

如果你以不可否认的类型通过,你就会看到,这一表述在它中具有转变功能,似乎就是问题。 我确信,与我一样,你也有兴趣如何使这一职能成为无效的类型。





相关问题
Multiple Where clauses in Lambda expressions

I have a simple lambda expression that goes something like this: x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty) Now, if I want to add one more where clause to the ...

Lambda Expressions and Event Subscriptions

I ve heard that if lambda expressions are used to subscribe to an event, then this creates a weak reference to the event handler code, so it is not required to explicitly unsubscribe from the event ...

Weak event handler model for use with lambdas

OK, so this is more of an answer than a question, but after asking this question, and pulling together the various bits from Dustin Campbell, Egor, and also one last tip from the IObservable/Rx/...

Is there a way to specify an "empty" C# lambda expression?

I d like to declare an "empty" lambda expression that does, well, nothing. Is there a way to do something like this without needing the DoNothing() method? public MyViewModel() { SomeMenuCommand =...

C# Lambda expression -Help

I am learning lambda expression and delegates.While i try to execute the following ,I am getting error at the line which is marked bold line. (Error : Operator += cannot be applied to operands of ...

LINQ to append to a StringBuilder from a String[]

I ve got a String array that I m wanting to add to a string builder by way of LINQ. What I m basically trying to say is "For each item in this array, append a line to this StringBuilder". I can do ...

热门标签