相关疑难解决方法(0)

将Linq表达式"obj => obj.Prop"转换为"parent => parent.obj.Prop"

我有一个类型的现有表达Expression<Func<T, object>>; 它包含像cust => cust.Name.

我还有一个带有类型字段的父类T.我需要一个接受上面作为参数的方法,并生成一个新的表达式,将父类(TModel)作为参数.这将用作MVC方法的表达式参数.

因此,cust => cust.Name成为parent => parent.Customer.Name.

同样,cust => cust.Address.State成为parent => parent.Customer.Address.State.

这是我的初始版本:

    //note: the FieldDefinition object contains the first expression
    //described above, plus the MemberInfo object for the property/field
    //in question
    public Expression<Func<TModel, object>> ExpressionFromField<TModel>(FieldDefinition<T> field)
        where TModel: BaseModel<T>
    {
        var param = Expression.Parameter(typeof(TModel), "t");

        //Note in the next line "nameof(SelectedItem)". This is a reference
        //to the property in …
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net-mvc linq-expressions

8
推荐指数
1
解决办法
572
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

linq ×1

linq-expressions ×1