Home > Back-end >  linq expression for ExecuteUpdateAsync
linq expression for ExecuteUpdateAsync

Time:11-23

I have found ExecuteDeleteAsync and ExecuteUpdateAsync in EF Core 7 with great enthusiasm. They help to make my code much simpler and faster. There is no need to use self-made procedures for batch delete or update of 1-2 fields. Anyway I have situations when the exact table and field of database for update should be selected in run time.

I can use the database table:

public static IQueryable<object> Set(this DbContext context, Type entity) =>
        context.ClrQuery(context.ClrType(entity));

I have the method to make expression to filter rows:

public static IQueryable Where(this IQueryable source, string equalProperty, object value, [NotNull] Type EntityType)
{
    PropertyInfo? property = EntityType.GetProperty(equalProperty);
    if (property == null)
        throw new NotImplementedException($"Type {EntityType.Name} does not contain property {equalProperty}");

    ParameterExpression parameter = Expression.Parameter(EntityType, "r");
    MemberExpression member = Expression.MakeMemberAccess(parameter, property);
    LambdaExpression whereExpression = Expression.Lambda(Expression.Equal(member, Expression.Constant(value, property.PropertyType)), parameter);
    MethodCallExpression resultExpression = WhereCall(source, whereExpression);
    return source.Provider.CreateQuery(resultExpression);
}

So I can find the rows to make update using

IQueryable Source = db.Set(EntityType).Where(FieldName, FieldValue, EntityType);
            

I should make expression to update IQueryable ExecuteUpdateQuery = Source.ExecuteUpdateAsync(EntityType, FieldName, FieldValue);

What is the way to access to expression for SetProperty?

CodePudding user response:

Try the following extensions. I have also corrected method signature:

var affected = anyQuery.ExecuteUpdate(FieldName, FieldValue);

var affected = await anyQuery.ExecuteUpdateAsync(FieldName, FieldValue, cancellationToken);

And implementation:

public static class DynamicRelationalExtensions
{
    static MethodInfo UpdateMethodInfo =
        typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdate));

    static MethodInfo UpdateAsyncMethodInfo =
        typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdateAsync));

    public static int ExecuteUpdate(this IQueryable query, string fieldName, object? fieldValue)
    {
        var updateBody = BuildUpdateBody(query.ElementType, fieldName, fieldValue);

        return (int)UpdateMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody });
    }

    public static Task<int> ExecuteUpdateAsync(this IQueryable query, string fieldName, object? fieldValue, CancellationToken cancellationToken = default)
    {
        var updateBody = BuildUpdateBody(query.ElementType, fieldName, fieldValue);

        return (Task<int>)UpdateAsyncMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody, cancellationToken })!;
    }

    static LambdaExpression BuildUpdateBody(Type entityType, string fieldName, object? fieldValue)
    {
        var setParam = Expression.Parameter(typeof(SetPropertyCalls<>).MakeGenericType(entityType), "s");
        var objParam = Expression.Parameter(entityType, "e");

        var propExpression = Expression.PropertyOrField(objParam, fieldName);
        var valueExpression = ValueForType(propExpression.Type, fieldValue);

        // s.SetProperty(e => e.SomeField, value)
        var setBody = Expression.Call(setParam, nameof(SetPropertyCalls<object>.SetProperty),
            new[] { propExpression.Type }, Expression.Lambda(propExpression, objParam), valueExpression);

        // s => s.SetProperty(e => e.SomeField, value)
        var updateBody = Expression.Lambda(setBody, setParam);

        return updateBody;
    }

    static Expression ValueForType(Type desiredType, object? value)
    {
        if (value == null)
        {
            return Expression.Default(desiredType);
        }

        if (value.GetType() != desiredType)
        {
            value = Convert.ChangeType(value, desiredType);
        }

        return Expression.Constant(value);
    }
}

CodePudding user response:

Thank you, it works perfectly.

  • Related