简体   繁体   English

如何获得表达式<system.func<tentity, tentity> &gt; 当我有一个表达式? </system.func<tentity,>

[英]How do I get Expression<System.Func<TEntity, TEntity>> when I have an Expression?

I'm working on a generic PATCH method for a service.我正在研究服务的通用 PATCH 方法。

public virtual void Patch ( int id, Delta<TEntityView> view )
{
    var type = typeof( TEntity );
    TEntity model = Activator.CreateInstance( type ) as TEntity;

    foreach( var changedProperty in view.GetChangedPropertyNames() )
    {
        var property = type.GetProperty( changedProperty );
        var propertyType = property.PropertyType;
        var retreived = view.TryGetPropertyValue( changedProperty, out object propval );

        if ( retreived && property != null )
        {
            property.SetValue( model, propval, null );
        }
    }

    UnitOfWork.Query<TEntity>( e => e.Id == id )
        .Update( m => model );
}

At the Update statement, I'm gettingUpdate声明中,我得到

System.Exception: 'Invalid Cast. The update expression must be of type MemberInitExpression.'

That method is defined in Entity Framework Plus ( https://entityframework-plus.net/ ):该方法在 Entity Framework Plus ( https://entityframework-plus.net/ ) 中定义:

#region Assembly Z.EntityFramework.Plus.EF6, Version=1.12.14.0, Culture=neutral, PublicKeyToken=59b66d028979105b
public static int Update<T>(this IQueryable<T> query, Expression<Func<T, T>> updateFactory) where T : class;

I changed my method as follows:我改变了我的方法如下:

public virtual void Patch ( int id, Delta<TEntityView> view )
{
    var type = typeof( TEntity );
    var bindings = new List<MemberBinding>();

    foreach( var changedProperty in view.GetChangedPropertyNames() )
    {
        var property = type.GetProperty( changedProperty );
        var propertyType = property.PropertyType;
        var retreived = view.TryGetPropertyValue( changedProperty, out object propval );

        if ( retreived && property != null )
        {
            bindings.Add( Expression.Bind( type.GetMember( changedProperty )[0], Expression.Constant( propval ) ) );
        }
    }

    Expression ex = Expression.MemberInit( Expression.New( type ), bindings );

    // Expression <-- have this
    // Expression<Func<TModel, TModel>> updateFactory <-- need this

    UnitOfWork.Query<TEntity>( e => e.Id == id )
        .Update( ex );
}

Now, on the Update, I'm getting a red squiggly with the message: Argument 1: cannot convert from 'System.Linq.Expressions.Expression' to 'System.Linq.Expressions.Expression<System.Func<TEntity, TEntity>>'现在,在更新中,我收到一条红色波浪线消息: Argument 1: cannot convert from 'System.Linq.Expressions.Expression' to 'System.Linq.Expressions.Expression<System.Func<TEntity, TEntity>>'

I'm certain that I'm missing something small to make the magic work.我确定我错过了一些小东西来使魔术起作用。 What is it?它是什么?

You did the hard part and it looks correct on first glance, what you want is to wrap it in lambda so the part like x => ex:你做了最困难的部分,乍一看它看起来是正确的,你想要的是把它包装在 lambda 所以像 x => ex 的部分:

var parameter = Expression.Parameter(type,"x");
var lambda = Expression.Lambda<Func<TEntity,TEntity>>(ex, parameter);
UnitOfWork.Query<TEntity>( e => e.Id == id )
    .Update( lambda );

(written from memory so to be verified) (写自 memory 待验证)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态功能 <IQueryable<TEntity> ,IOrderedQueryable <TEntity> &gt;表达 - Dynamic Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> Expression 在表达式中返回 TEntity <Func<TEntity, TResult> &gt; - Return TEntity in Expression<Func<TEntity, TResult>> 转换表达式 <Func<TEntity,TKey> 表达 <Func<TEntity, Object> &gt; - Convert Expression<Func<TEntity,TKey> to Expression<Func<TEntity, Object>> 如何映射表达式 <Func<TEntity, bool> &gt;表达 <Func<TDbEntity, bool> &gt; - How to map Expression<Func<TEntity, bool>> to Expression<Func<TDbEntity, bool>> 表达式 OrderBy Then 通过返回 (Func <iqueryable<tentity> , IOrderedQueryable<tentity> &gt;) </tentity></iqueryable<tentity> - Expression OrderBy Then By returning (Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>) 创建表达式<Func<TEntity,object> &gt; 动态调用聚合到 IQueryable<TEntity> - Create Expression<Func<TEntity,object>> dynamically and call Aggregate to IQueryable<TEntity> 我如何使用CreateObjectSet <TEntity> TEntity作为参数在哪里? - How do I use CreateObjectSet<TEntity> where TEntity as a parameter? 如何将多个属性传递给表达式<Func<TEntity, IEnumerable<TProperty> &gt;&gt; - How to pass multiple properties to Expression<Func<TEntity, IEnumerable<TProperty>>> 具有精确表达式的Moq <Func <TEntity,bool >> - Moq with exact Expression<Func<TEntity, bool>> 套用表情 <Func<TEntity, bool> &gt;在通用存储库中? - Apply Expression<Func<TEntity, bool>> in Generic Repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM